Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to bind two DropDownList into a GridView in ASP.NET.

I did bind one DropDownList with Databound event, but the problem is how can I bind the second DropDownList in the GridView?

One DropDownList completely binds with database, but the other does not bind with the same event or GridView.

My code:
C#
public DataTable FetchCustomerType()
   {
       string sql = "Select Distinct type From cust_info";
       SqlDataAdapter da = new SqlDataAdapter(sql, conn);
       DataTable dt = new DataTable();
       da.Fill(dt);
       return dt;
   }

   public DataTable FetchCustomerGender()
   {
       string sql = "Select Distinct gender From cust_info";
       SqlDataAdapter da = new SqlDataAdapter(sql, conn);
       DataTable dt = new DataTable();
       da.Fill(dt);
       return dt;
   }

   protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
   {
       if (e.Row.RowType == DataControlRowType.DataRow)
       {
           DropDownList cmbtype = (DropDownList)e.Row.FindControl("cmbType");
           DropDownList cmbgender = (DropDownList)e.Row.FindControl("cmbGender");
           if (cmbtype != null && cmbgender !=null)
           {
               cmbtype.DataBind();
               cmbgender.DataBind();
               cmbtype.DataSource = FetchCustomerType();
               cmbgender.DataSource = FetchCustomerGender();
               cmbtype.SelectedValue = GridView1.DataKeys[e.Row.RowIndex].Values[1].ToString();
               cmbgender.SelectedValue = GridView1.DataKeys[e.Row.RowIndex].Values[2].ToString();
           }
       }



       if (e.Row.RowType == DataControlRowType.Footer)
       {
           DropDownList cmbnewtype = (DropDownList)e.Row.FindControl("cmbNewType");
           DropDownList cmbnewgnder =(DropDownList)e.Row.FindControl("cmbNewGender");
           cmbnewtype.DataSource = FetchCustomerType();
           cmbnewgnder.DataSource = FetchCustomerGender();
           cmbnewtype.DataBind();
           cmbnewgnder.DataBind();
       }

   }

Where should I put the other DropDownList coding into this code which executes properly?

Thank you.
Posted
Updated 29-Feb-12 8:46am
v2
Comments
André Kraak 29-Feb-12 14:46pm    
Edited question:
Added pre tags
Formatted text/code
Spelling/Grammar

1 solution

Hi,
your code is fine but need some chanages into RowataBound Event
which is as below
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList cmbtype = (DropDownList)e.Row.FindControl("cmbType");
            DropDownList cmbgender = (DropDownList)e.Row.FindControl("cmbGender");
            if (cmbtype != null && cmbgender !=null)
            {
                cmbtype.DataSource = FetchCustomerType();
cmbtype.DataTextField = "type";
cmbtype.DataValueField = "type";
                cmbtype.DataBind();
                cmbtype.SelectedValue = GridView1.DataKeys
e.Row.RowIndex].Values[1].ToString();

    cmbgender.DataSource = FetchCustomerGender();
    cmbnewgnder.DataTextField = "gender";
    cmbnewgnder.DataValueField = "gender";

    cmbgender.DataBind();         
                cmbgender.SelectedValue = GridView1.DataKeys[e.Row.RowIndex].Values[2].ToString();
            }
        }
}
 
Share this answer
 
Comments
Anuja Pawar Indore 1-Mar-12 2:22am    
Added code block
djharry2 1-Mar-12 5:21am    
hello i hv problem in this code pls suggest me solution thx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900