Question

I have a gridview which I am using to update a database. The gridview consists of three columns. The first column is a ID number. The second column is a category (BU). The final column is a comments field. I have the edit set up to allow editing of the second and third column. The second column needs to have a dropdownlist for the user to select from. I am doing great up to this point. My client has added another group to this so the query that populates the dropdownlist needs to be modified to find whether the customer is cust A or cust B. I am basing the dropdown list on a view in the database. The view has two columns Cust, BU. I can get the cust from the original query that populates the gridview. I have tried to set it as a DataKey but I am not sure how to reference it.

Here is the code for the dropdownlist in the gridview. The object is tied to the gridview using onrowdatabound = "Rowdatabound"

Here is the rowdatabound code behind:

protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow && gvSummary.EditIndex == e.Row.RowIndex)
    {

        DropDownList dlBU = (DropDownList)e.Row.FindControl("dlBU");

        string query = "select distinct Unit from vw_BU";
        SqlCommand cmd = new SqlCommand(query);
        dlBU.DataSource = GetData(cmd);
        dlBU.DataTextField = "Unit";
        dlBU.DataValueField = "Unit";
        dlBU.DataBind();
        dlBU.Items.FindByValue((e.Row.FindControl("lblBU") as Label).Text).Selected = true;
    }
}

The lblBU is so that the dropdownlist will retain the original value if any. I added a null value to the database for each customer, so that if a value had not been entered I avoid the error.

So ultimately what I want to do is to have the select distinct Unit from vw_BU where cust = "Tim" and I would get Tim from a datakey in the original select statement.

I hope this makes sense.

Was it helpful?

Solution

NOTE: Checking GridView.EditIndex in OnRowCommand will always have its value as -1 and therefore checking gvSummary.EditIndex==e.Row.RowIndex will never be true.

You should place the code in OnRowEditing event of your gridview, rather than OnRowCommand. Start by setting the DataKeyNames property of your gridview.

<asp:GridView ID="gvSummary" DataKeyNames="Cust" ... />

In your OnRowEditing event, you can access this Cust value of the row to be edited.

<asp:GridView ID="gvSummary" DataKeyNames="Cust" 
     OnRowEditing="gvSummary_RowEditing"  ... />

// And your Code behind will have..

protected void gvSummary_RowEditing(object sender, GridViewEditEventArgs e)
{

        // Get the Cust column value 
        string _custName= gvSummary.DataKeys[e.NewEditIndex].Value.ToString();
        // get the value without using DataKeys as:
        string _custName2= gvSummary.Rows[e.NewEditIndex].Cells[2].Text;
       // frame your query now::
       string query = "select distinct Unit from vw_BU where Cust='"+_custName+"'"; 

       // rest of your code to bind dropdownlist  

}

EDIT:: In case you have set more than one Columns as DataKeys, you can access the values as:

 // using Names
    string str = gvSummary.DataKeys[e.NewEditIndex].Values["Cust"].ToString();
    // using index
    string str = gvSummary.DataKeys[e.NewEditIndex].Values[2].ToString();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top