Click here to Skip to main content
15,920,030 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to bind dropdownlist in gridview in row databound event in asp.net???please help me
Posted
Updated 20-Nov-17 23:49pm

Check this following code

protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
            DropDownList ddlDropDownList = (DropDownList)e.Row.FindControl("ddl1");
if (ddlDropDownList != null)
            {
                SqlDataAdapter da = new SqlDataAdapter("select distinct(source) from pickupdroptariff", con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    ddlDropDownList.DataSource = dt;
                    ddlDropDownList.DataTextField = "source";
                    ddlDropDownList.DataValueField = "source";
                    ddlDropDownList.DataBind();
                    ddlDropDownList.Items.Insert(0, "--Select--");

                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Source Not Available');", true);
                }
            }
}
 
Share this answer
 
Try this
C#
protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
 if (e.Row.RowType == DataControlRowType.DataRow)       
 {
    // your code to get data 
   // I assumed you are getting data in dataset using some query

   DropDownList ddlUserName = (DropDownList)e.Row.FindControl("ddlUserName");
   ddlUserName .DataSource = dataset.Tables[0].DefaultView;
   ddlUserName .DataValueField = "ValueField"; 
   ddlUserName .DataTextField = "TextField";
   ddlUserName .DataBind();
 }
}
 
Share this answer
 
C#
protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {        
           ddlDropDownList.DataSource = dt;
           ddlDropDownList.DataBind();
           ddlDropDownList.DataTextField = "Binds_Field_to_show";
           ddlDropDownList.DataValueField = "Binds_Field_of_Data";
           ddlDropDownList.Items.Insert(0, "--Select--");           
        }

    }
 
Share this answer
 
v2
Comments
[no name] 31-Aug-12 3:16am    
if i want to save my id from drop down list how it can be done
Don't you have access to google? These are the basics of Asp.Net. You could have tried searching the problem in google. If you are unable to do so or getting some problem then you should ask the question here.
Try this:
C#
protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
     if (e.Row is DataRow)       
     {
           DropDownList ddlTest = (DropDownList)e.Row.FindControl("ddlTest");
           ddlTest.DataSource = dt;//dt is your datatable with columns ID, Name
           ddlTest.DataValueField = "ID"; 
           ddlTest.DataTextField = "Name";
           ddlTest.DataBind();
     }
}




--Amit
 
Share this answer
 
Comments
Jitendra Singh 23-Aug-13 1:46am    
Hello to everybody,
My code raising an error for
"DropDownList ddlUserEmailid = (DropDownList)e.Row.FindControl("ddlUserEmailid");"
Error: Object reference not set to an instance of an object

I also tried this:
"DropDownList ddlUserEmailid = gridImport.FindControl("ddlUserEmailid") as DropDownList;"

Please suggest.

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