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:
Use this code to bound data to dropdown
without using RowDataBound
.
Create a function that'll bind the Data to dropdown
as follow and call it in Page_Load
event
Public void fill_gridView_dropDown()
{
// your connection and query to retrieve dropdown data will go here
// this loop will go through all row in GridView
foreach(GridViewRow row in your_gridView_Name.Rows) {
DropDownList dropDown = (DropDownList)row.FindControl("dropDownList_id");
dropDown.DataSource = dataSource;
dropDown.DataValueField = "ValueField";
dropDown.DataTextField = "TextField";
dropDown.DataBind();
}
}
Please note that you have to bind
GridView first, and then you have to bind your dropdown