Name' row' does not exist in current context
I am trying to execute below code and i am getting an error saying :
Name' row' does not exist in current context
Code
protected void wgrdSearchResult_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "edit")
{
int index = Convert.ToInt32(wgrdSearchRes开发者_开发百科ult.DataKeys[row.RowIndex].Value);
//int index = Int32.Parse((string)e.CommandArgument);
string CustomerID = (string)wgrdSearchResult.DataKeys[index].Values["CustomerID"];
}
}
row is not declared in your method. Look at this MSDN example showing what you seem to be trying to do. From above link:
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button clicked
// by the user from the Rows collection.
GridViewRow row = ContactsGridView.Rows[index];
You haven't declared any variable named row
, so your assignment to index
is failing.
Try this:
protected void wgrdSearchResult_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "edit")
{
int index = Convert.ToInt32(wgrdSearchResult.DataKeys[e.CommandArgument].Value);
string CustomerID = (string)wgrdSearchResult.DataKeys[e.CommandArgument].Values["CustomerID"];
}
}
Can you show the code for the front-end of the GridView as well? My code assumes that you either have a single DataKey (that is, CustomerID), or that you have multiple data keys set up for your GridView, where the first data key is some arbitrary "index" that is apparently meaningless and never used while the second key is a meaningful CustomerID.
精彩评论