问题
In my application, I give the user the option to search the database. When they click edit, I want the GridView to only show the row they are updating by storing its ID.
I'm trying the following code, but it is not returning anything other than the Control.
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
string value = GridView1.Rows[e.NewEditIndex].Cells[3].ToString();
}
Any ideas on how to store the value that I'm looking for?
回答1:
Use the cell's Text property
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
string value = GridView1.Rows[e.NewEditIndex].Cells[3].Text;
// or
string value = GridView1.Rows[e.NewEditIndex].Cells[3].Value;
}
Or If you have declared the control as <TemplateField> for which you are trying to get the value for then try
string value = ""
Label lblID = (Label)GridView1.Rows[e.NewEditIndex].FindControl("lblID");
value = lblID.Text;
Another alternative would be to add DataKeyNames="ID" for the gridview which you can retrieve using
// assuming that the value of your datakey is numeric value
long Id = long.Parse(GridView1.DataKeys[e.NewEditIndex].Values["ID"].ToString());
Note you can add multiple comma separated values to DataKeyNames property.
来源:https://stackoverflow.com/questions/25128461/store-id-of-rowediting-instead-of-index