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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Pankaj RawatPosted Mar 23, 2013, 3:39 AM
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.
Vishal GilbilePosted Mar 23, 2013, 3:10 AM
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.
Satyapriya NayakPosted Mar 23, 2013, 3:05 AM