how to bind a dropdownlist in gridview?

gridview dropdownlist bind selected value
how to add dropdownlist in gridview in asp.net dynamically
how to get selected value from dropdownlist in gridview in asp.net c#
how to display selected data from dropdownlist in gridview in c# language
aspsnippets gridview dropdownlist
how to bind dropdownlist in c# dynamically
bind datarow to dropdownlist c#
bind dropdownlist in datagrid asp net

I have a gridview in which every row contains a dropdownlist. I want to bind every dropdownlist dynamically. Can someone tell me how can i do it. Thanks in Advance

If you are using template column then you can bind your drop-down from mark-up using data-binding expressions. For example,

<asp:TemplateField HeaderText="XYZ">
  <ItemTemplate>
    <asp:DropDownList runat="server" ID="MyDD" DataSourceId="MyDataSource" />
  </ItemTemplate> 
</asp:TemplateField>

Above is assuming that your drop-down data in constant across rows. If it is changing then you can use data-binding expression such as

<asp:DropDownList runat="server" DataSource='<%# GetDropDownData(Container) %>' DataTextField="Text" DataValueField="Value"  />

GetDropDownData will be a protected method in code-behind that will return the data (data-table, list, array) for the given row.

You can use GridView.RowDataBound event (or RowCreated event) in code-behind to fill drop-downs. For example,

  protected void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  {

    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      // Find the drop-down (say in 3rd column)
      var dd = e.Row.Cells[2].Controls[0] as DropDownList;
      if (null != dd) {
         // bind it
      }

      /*
      // In case of template fields, use FindControl
      dd = e.Row.Cells[2].FindControl("MyDD") as DropDownList;
      */
    }

  }

how to bind a dropdownlist in gridview?, Step 2: In Solution Explorer you will get your empty website, Add a web form, SQL Database. This will allow you access the properties of dropdownlist placed inside the GridView Template column. drdList = ( DropDownList )( GridView1.Rows[ grdRow.RowIndex ].Cells[4].FindControl( "ddl" )); // DataBinding of nested DropDownList Control for each row of GridView Control.

In addition to the proposed methods, you may also bind your controls within your markup, in this way:

<asp:GridView ID="MyGrid" runat="server" DataSourceID="MyDataSource1">
    <Columns>
        <asp:TemplateField>
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Bind ("CustomerId") %>' DataSourceID="CustomersDataSource" DataTextField="CustomerName" DataValueField="CustomerId" >
                </asp:DropDownList>
            </EditItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

How to populate DropDownList in GridView in ASP.Net, If you are using template column then you can bind your drop-down from mark-up using data-binding expressions. For example, The RowDataBound event of the ASP.Net GridView is used to bind the ASP.Net DropDownList with the Countries form the Northwind database, once the DropDownList is populated, the Country to which the Customer belongs is set as the Selected in the DropDownList.

Here is your gridview

<asp:GridView ID="grvExcelData" runat="server" onrowdatabound="GridView2_RowDataBound">
    <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
      <Columns>
          <asp:TemplateField>
              <ItemTemplate>
                 <asp:DropDownList ID="DrdDatabase" Width="100px" runat="server">
                 </asp:DropDownList>
              </ItemTemplate>
          </asp:TemplateField>
      </Columns>
  </asp:GridView>

and your RowDataBound event for the gridview would be

    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       string cities = "maxico,chennai,newdelhi,hongkong";
       string [] arr = cities.Split(',');
    // Instead of string array it could be your data retrieved from database.
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList ddl = (DropDownList)e.Row.FindControl("DrdDatabase");
            foreach (string colName in arr )
                ddl.Items.Add(new ListItem(colName));
        }
    }

how to bind dropdownlist in gridview from database in asp.net c# 4.6 , Here Mudassar Ahmed Khan has explained with an example and attached sample code, how to to bind DropDownList placed inside ItemTemplate of  An Asp.Net DropDownList control holds a list of data, which allows the user to select a single data from a drop down list. We either use this as a standalone control on a web page or embed it in another control, like a GridView control.

protected void gvSalesAppData_RowDataBound(Object sender, GridViewRowEventArgs e)

    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            DropDownList ddlCurrentPhase = (DropDownList)e.Row.FindControl("ddlCurrentPhase");
            DropDownList ddlProductFamily = (DropDownList)e.Row.FindControl("ddlProductFamily");
            DropDownList ddlProductGroup = (DropDownList)e.Row.FindControl("ddlProductGroup");
            DropDownList ddlETProgramManager = (DropDownList)e.Row.FindControl("ddlETProgramManager");
            DropDownList ddlPLMForTheProduct = (DropDownList)e.Row.FindControl("ddlPLMForTheProduct");

            TrackingToolObj.BindCurrentPhases(ddlCurrentPhase);
            TrackingToolObj.BindCurrentPhases(ddlProductFamily);
            TrackingToolObj.BindProductGroups(ddlProductGroup);
            TrackingToolObj.GetEmployeesBasedOnRoleTypeId(ddlETProgramManager, (int)OSAEnums.RoleTypes.ProgramManager, false);
            TrackingToolObj.GetEmployeesBasedOnRoleTypeId(ddlPLMForTheProduct, (int)OSAEnums.RoleTypes.PLM, false);


        }

    }

how to bind dropdownlist in gridview in row databound event in asp , Check this following code. Hide Expand Copy Code. protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e  Today, I have provided an article showing you how to bind a DropDownList with a database and display the bound data in a GridView in ASP.NET. In this article, we create a table in a SQL Server database and bind that table with a DropDownList control. After that we select an item in the DropDownList and match with the database table for the records that match with the database. It will display

Binding the GridView

Below is the code to Bind the GridView control with data.

C#

protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostBack) 
   {
     this.BindData();   
   }
} 

private void BindData()
{
   string query = "SELECT top 10 * FROM Customers";    
   SqlCommand cmd = new SqlCommand(query);    
   gvCustomers.DataSource = GetData(cmd);    
   gvCustomers.DataBind(); 
}

private DataTable GetData(SqlCommand cmd)
{    
    string strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;    
    using (SqlConnection con = new SqlConnection(strConnString))   
    {        
       using (SqlDataAdapter sda = new SqlDataAdapter())     
       {            
          cmd.Connection = con;            
          sda.SelectCommand = cmd;            
          using (DataTable dt = new DataTable())  
          {                
              sda.Fill(dt);                
              return dt;
          }     
       }   
    }
}

Bind a DropDownList to a Database Table using GridView in Asp , An Asp.Net DropDownList control holds a list of data, which allows the user to select a single data from a drop down list. We either use this as a standalone  Here I will explain how to populate or bind dropdownlist selected value in gridview edititemtemplate in asp.net using c#, vb.net with example or set selected value of dropdownlist in gridview from database in asp.net using c#, vb.net with example.

Bind data to DropDownList in GridView, This example shows you how to bind data to DropDownList in GridView based on the Grid Row Value. In this example we will bind the States to DropDownList  In order to bind a DropDownList, we need a DropDownList control and an SqlDataSource to connect the datafield to the database. The following code create a TemplateField for city column in the webpage. Also we need another SqlDataSource to connect the DropDownList to the database.

DropDownList in GridView, DropDownList in GridView. The GridView allows editing on a row-by-row basis. In the previous lesson we saw how to edit a Gridview using SqlDataSource. Initial chamber Step 1: Open Visual Studio 2010 and create an empty Website. Give a suitable name [gridview_demo]. Step 2: In Solution Explorer you will get your empty website, Add a web form, SQL Database.

data bind to dropdownlist inside gridview, Hi, in this sol the data is binding to DDL.but it is not loading as the value of of that row. means suppose m & F r there. in data base there is m. Steps to bind drop-down list in Gridview while Editing. 1) Add Html markup i.e (Gridview) 2) On Page load bind Gridview control. 3) On RowDataBound bind Dropdownlist.

Comments
  • What about the RowCreated event? Would there be a reason to prefer one over the other?
  • @Tim, RowCreated will also work. However, I believe that if you are binding the grid only first time (and not on post-back), RowCreated will certainly fire on every post-back but not RowDataBound (I am not 100% sure here). So in such case, we can rely on view-state to get drop-down to filled up. Personally, I prefer markup route.
  • This is assumed that you have your SQL instance declared in your code. If you already bound your grid view in your code behind then there needs to be a different solution, because you would not be able to use DataSourceId="MyDataSource"
  • Can you show how this would update and display in the label of the ItemTemplate?
  • This does not look like this is bound to a database but instead attached to an array of cities. If a user makes a change to the drop-down how will this update the database?
  • please edit your answer and format the code to make it readable
  • To be honest, that's a daft way of doing it. If you have 500 rows in a gridview, you are going to query a database 500 times? When I have to do this, I fill a dataset with the data for the items in the drop down list and, as shown above, in the RowDataBound event, bind the DataSet to the dropdownlist(s).