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();