Write this code in the RowDataBound event of the gridview,
Alternatively,
DropDownList ddl = (DropDownList)e.Row.Cells[0].FindControl("controlname");
ddl.SelectedValue = DataBinder.Eval(e.Row.DataItem, "db_field").ToString();
if (sender is GridView)
{
GridView grid = (GridView)sender;
grid.SelectedIndex = e.Row.RowIndex;
if (!string.IsNullOrEmpty(grid.SelectedDataKey["db_field"].ToString()))
{
DropDownList ddl = (DropDownList)e.Row.Cells[0].FindControl("controlname");
ddl.SelectedValue = grid.SelectedDataKey["db_field"].ToString();
}
grid.SelectedIndex = -1;
}
{
GridView grid = (GridView)sender;
grid.SelectedIndex = e.Row.RowIndex;
if (!string.IsNullOrEmpty(grid.SelectedDataKey["db_field"].ToString()))
{
DropDownList ddl = (DropDownList)e.Row.Cells[0].FindControl("controlname");
ddl.SelectedValue = grid.SelectedDataKey["db_field"].ToString();
}
grid.SelectedIndex = -1;
}
Alternatively,
DropDownList ddl = (DropDownList)e.Row.Cells[0].FindControl("controlname");
ddl.SelectedValue = DataBinder.Eval(e.Row.DataItem, "db_field").ToString();
No comments:
Post a Comment