2011-08-29 69 views
0

我试图执行下面的代码,我得到一个错误说:名称“行”并不在当前的背景下存在

名称“行”并不在当前的背景下存在

代码

protected void wgrdSearchResult_RowCommand(object sender, GridViewCommandEventArgs e) 
     { 

      if (e.CommandName == "edit") 
      { 
       int index = Convert.ToInt32(wgrdSearchResult.DataKeys[row.RowIndex].Value); 

       //int index = Int32.Parse((string)e.CommandArgument); 
       string CustomerID = (string)wgrdSearchResult.DataKeys[index].Values["CustomerID"]; 
      } 


     } 

回答

0

您尚未申报名为row的任何变量,因此您对index的分配失败。

1

行未在您的方法中声明。看看这个MSDN的例子,显示你似乎正在尝试做什么。 从上面的链接:

// 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]; 
+0

+1首先提到CommandArgument,它听起来像一个计划。 – CharithJ

0

试试这个:

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"]; 
    } 
} 

你可以显示在GridView的前端以及代码?我的代码假设你有一个DataKey(即CustomerID),或者你为GridView设置了多个数据键,其中第一个数据键是一些任意的“索引”,显然没有意义,并且从未使用第二个关键是一个有意义的CustomerID。

相关问题