Monday, March 5, 2012

Setting the selected value of dropdownlist in gridview on databind

Write this code in the RowDataBound event of the gridview,
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;
}

Alternatively,

DropDownList ddl = (DropDownList)e.Row.Cells[0].FindControl("controlname");
ddl.SelectedValue = DataBinder.Eval(e.Row.DataItem, "db_field").ToString();

No comments:

Codeigniter Shield Authorization

Codeigniter Shield Authorization CodeIgniter Shield is the official authentication and authorization framework for CodeIgniter 4. It provide...