I have a DropDownList that I would like to populate with column values from a DataBase. However, when I try to bind the DropDownList in code behind, the IDE keeps telling me:
You can't directly populate GridView's
dropdownlist
like this. You need to set datasource of GridView
first i.e.
GridView1.DataSource = DataSource
And if you would like to access dropdownlist
of this gridview, you can use RowDataBound
event handler of GridView
i.e.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//Checking whether the Row is Data Row
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Finding the Dropdown control.
Control ctrl = e.Row.FindControl("EqpCatDDL");
if (ctrl != null)
{
DropDownList dd = ctrl as DropDownList;
List lst = new List();
dd.DataSource = lst;
dd.DataBind();
}
}
}