Hi!!!
How to bind dropdown values in gridview in asp.net c#????
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Satyapriya NayakRank #47 · 31.5K reputationPosted Mar 22, 2013, 11:05 PM EDT
Vishal GilbileRank #268 · 9.4K reputationPosted Mar 22, 2013, 11:10 PM EDT
Binding Control inside gridview depends where your control resides. I mean whether it resides inside ItemTemplate or EditItemTemplate or Footer Template. Taking it one by one Following thing you will have to do.
If your controls reside inside FooterTemplate. Lets assume I've following fields names (ID,Name,Country etc) on my gridview. I've have dropdown for my country field.
In that case I'll write the below code
private void LoadCountry()
{
DropDownList ddl=(DropDownList)gridview1.FooterRow.Cell[2].FindControl("Name of your dropdown control on footer row");
// and finally your code to access the data;
ddl.DataSource=ds.Tables[0].DefaultView;
ddl.DataValueField="ColumnName";
ddl.DataTextField="ColumnName";
ddl.DataBind();
}
If your control reside inside EditItemTemplate then the following code could be written inside GridView_RowEditing event.
protected void gridview1_RowEditing(object sender,====)
{
gridview1.EditIndex=e.NewIndex;
DropDownList ddl=(DropDownList)gridview1.Rows[e.NewIndex].Cell[2].FindControl("Name of your dropdown control on footer row");
// and finally your code to access the data;
ddl.DataSource=ds.Tables[0].DefaultView;
ddl.DataValueField="ColumnName";
ddl.DataTextField="ColumnName";
ddl.DataBind();
}
Hope that solves your problem.
With Regards,
Vishal Gilbile.
Pankaj Rawat71 reputationPosted Mar 22, 2013, 11:39 PM EDT
lets assume your grid id="grid1".
and your dropdown is ddmonth.
and you have to bind month in dropdown
then create rowdatabound event for it.and in code behind
protected void grid1.DataBound(Object sender,GridViewRowEventArgs e)
{
DataRowView gridrow = (DataRowView)e.Row.DataItem;
DropDownList ddlmonth= (DropDownList)e.Row.FindControl("ddmonth");
if(!ddlmonth)
{
string[] months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
for(int i=0;i
ddlmonth.items.add(months(i));
ddlmonth.items[i].value=(i+1).tostring();
listitem item_Obj=new Listitem("Select","0");
ddlmonth.items.insert(0,item_Obj);
}
}
}
Similarly you can retrieve data from your datasource and bind it to dropdown.