.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Bind Dropdown inside gridview in CSharp

Posted By:shankey       Posted Date: April 30, 2011    Points: 200    Category: ASP.NET    URL: http://www.dotnetspark.com  

In this article I will explain how to Bind Dropdownlist control inside gridview in asp.net website/webapplication using C#
 

Bind Dropdown inside grid view in CSharp

Step1:  Create data table for Grid view and Dropdown list

For grid view control

    
 
For dropdown list control



Step2:  Bind data table to grid view control



Step3:  On RowDataBound

Event grab the dropdown control from the row and bind the desired data table to the dropdown list



Step4: Run application


Full C# code used in above example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


public partial class _Default : System.Web.UI.Page
{
    public DataTable dtDay = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //Creating static datatable
            DataTable dt = new DataTable();
            dt.Columns.Add("month");

            //Adding rows
            dt.Rows.Add("January");
            dt.Rows.Add("February");
            dt.Rows.Add("March");
            dt.Rows.Add("April");
            dt.Rows.Add("May");
            dt.Rows.Add("June");
            dt.Rows.Add("July");
            dt.Rows.Add("August");
            dt.Rows.Add("September");
            dt.Rows.Add("October");
            dt.Rows.Add("November");
            dt.Rows.Add("December");

            //Binding to grid
            gv.DataSource = dt;
            gv.DataBind();


        }


    }
    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList drpDay = (DropDownList)e.Row.FindControl("drpDay");
            drpDay.DataSource = dtDay;
            drpDay.DataTextField = "Day";
            drpDay.DataValueField = "Day";
            drpDay.DataBind();

            drpDay.SelectedIndex = e.Row.RowIndex;
        }
        else if (e.Row.RowType == DataControlRowType.Header)
        {
            //Creating static datatable
            dtDay.Columns.Add("Day");
            //Adding rows
            dtDay.Rows.Add("1");
            dtDay.Rows.Add("2");
            dtDay.Rows.Add("3");
            dtDay.Rows.Add("4");
            dtDay.Rows.Add("5");
            dtDay.Rows.Add("6");
            dtDay.Rows.Add("7");
            dtDay.Rows.Add("8");
            dtDay.Rows.Add("9");
            dtDay.Rows.Add("10");
            dtDay.Rows.Add("11");
            dtDay.Rows.Add("12");
            dtDay.Rows.Add("13");
            dtDay.Rows.Add("14");
            dtDay.Rows.Add("15");
            dtDay.Rows.Add("16");
            dtDay.Rows.Add("17");
            dtDay.Rows.Add("18");
            dtDay.Rows.Add("19");
            dtDay.Rows.Add("20");
            dtDay.Rows.Add("21");
            dtDay.Rows.Add("22");
            dtDay.Rows.Add("23");
            dtDay.Rows.Add("24");
            dtDay.Rows.Add("25");
            dtDay.Rows.Add("26");
            dtDay.Rows.Add("27");
            dtDay.Rows.Add("28");
            dtDay.Rows.Add("29");
            dtDay.Rows.Add("30");
        }
    }
}

ASPX page code containg Gridview with Dropdownlist box inside Gridview control

   
            
                
                    
                        
                    
                    
                        
                    
                
                
                    
                        
                    
                    
                        
                        
                    
                
            
        

You can also download the demo project of the above example

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend