Can someone pls help me ? I have an ASP.NET page with Gridview(with EDIT and Delete functionlity) and dropdownlist inside it, the Dropdownlist is well populated with the correct values but whenever I click EDIT button in the gridview the selected value in DropDownlist is set to the wrong value(to the first item in the collection) . The data comes from the DB and I am using LIST<T> to pupulate it.
Just to mention that I am Binding Gridview in page_load() event.
Here is the code:
ASP.NET page
<EditItemTemplate>
<asp:DropDownList ID="ddlVehicleNr" runat="server"
AppendDataBoundItems="true">
</asp:DropDownList>
</EditItemTemplate>
Code Behind Page
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Control ctrl = e.Row.FindControl("ddlVehicleNr");
if (ctrl != null)
{
DropDownList ddl = (DropDownList)ctrl;
VehicleList vehicle = new VehicleList();
List<Vehicle> lv = InVehicle.GetList(vehicle);
ddl.DataSource = lv;
ddl.DataTextField = "VehicleName";
ddl.DataValueField = "VehicleID";
if (ddl != null)
{
ddl.SelectedValue = ddl.DataValueField;
ddl.DataBind();
}
}