How to show a specific item in dropdownlist based on specific value of a cell of a table-row in asp.net gridview control?

StackOverflow https://stackoverflow.com/questions/23625590

Question

i am new to asp.net. I am building an admin page and i want to display employee's data in GridView by fetching it from database table. Table has 3 columns (id, name, isManager). There are three possible values for "isManager" column. These values are "yes", "no" and "null". The admin has right to decide for an employee to make him a manager by selecting "yes" or "no" from DropDownList.

This admin page has a GridView control that contains two BoundFileds (i.e. id & name) and one template field (i.e. DropDownList). I am having difficulty in displaying "isManager" column values in DropDownList. I want DropDownList to display selected value/text as "yes" if database table-row has "yes" in "isManager" column, "no" if there is "no" in table-row and display an item "Select Choice" if table-row contains a null value.

My code:

 protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
 {
   string query = "select * from tblUsersTable";
   DataSet ds = DataBaseConnectivity.GetData(query);
   GridView1.DataSource = ds;
   GridView1.DataBind();
  }
 }

 // RowDataBound() method

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
 {
   if (e.Row.RowType == DataControlRowType.DataRow)
     {
       //Find the DropDownList in the Row
      DropDownList ddlManager = (e.Row.FindControl("ddlManager") as DropDownList);
      DataSet ds = DataBaseConnectivity.GetData
                                 ("select distinct [isManager] from tblUsersTable");
      ddlManager.DataSource = ds; 
      ddlManager.DataTextField = "isManager";
      ddlManager.DataValueField = "isManager";
      ddlManager.DataBind();
      ddlManager.Items.Insert(0, new ListItem("Please select","-1"));

      /* After these lines of code i am not finding the right way to implement my logic
        */

Help me to figure it out.

Was it helpful?

Solution

You have to use the DataItem of the GridViewRow to access the underyling record. Then you can select the corect item via DropDownList.SelectedValue:

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
 {
   if (e.Row.RowType == DataControlRowType.DataRow)
     {
       //Find the DropDownList in the Row
      DropDownList ddlManager = (e.Row.FindControl("ddlManager") as DropDownList);
      DataSet ds = DataBaseConnectivity.GetData
                                 ("select distinct [isManager] from tblUsersTable");
      ddlManager.DataSource = ds; 
      ddlManager.DataTextField = "isManager";
      ddlManager.DataValueField = "isManager";
      ddlManager.DataBind();
      ddlManager.Items.Insert(0, new ListItem("Please select","-1"));
      DataRow row = ((DataRowView)e.Row.DataItem).Row;
      bool isManager= row.Field<bool>("isManager"); // use the correct type if it's not bool
      ddlManager.SelectedValue = isManager.ToString();

Apart from that, i would not use such db-helper classes like DataBaseConnectivity in ASP.NET. They are just a source for nasty errors or performance issues, all the more if you use a static connection. Further informations here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top