I am trying to do this as asked earlier. The only difference that I found is additional List item that was included in above code.
I tried to use AppendDataBou
The use of the GridView_DataBound event handler solves the problem.
In your case you will need to add a HiddenField to store the PK_DepartmentId value:
protected void gvExample_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow gvRow in gvExample.Rows)
{
DropDownList ddlDepartment = gvRow.FindControl("ddlDepartment_Edit") as DropDownList;
HiddenField hfDepartmentId = gvRow.FindControl("hfDepartmentId") as HiddenField;
if (ddlDepartment != null && hfDepartmentId != null)
{
ddlDepartment.SelectedValue = hfDepartmentId.Value;
}
}
}