【问题标题】:Name' row' does not exist in current context当前上下文中不存在名称“行”
【发布时间】:2011-08-29 03:44:26
【问题描述】:

我正在尝试执行以下代码,但我收到一条错误消息:

当前上下文中不存在名称“行”

代码

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


        }

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    row 未在您的方法中声明。看看这个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];
    

    【讨论】:

    • +1 表示首先提到 CommandArgument,这听起来像是一个计划。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      试试这个:

      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。

      【讨论】:

        猜你喜欢
        • 2013-10-02
        • 2014-04-22
        • 2017-06-17
        • 2015-09-11
        • 1970-01-01
        • 1970-01-01
        • 2022-12-18
        • 2020-11-02
        相关资源
        最近更新 更多