代码之家  ›  专栏  ›  技术社区  ›  Developer

GridView中的错误

  •  1
  • Developer  · 技术社区  · 14 年前

    我把代码写在按钮下面,如下

     string actkey = string.Empty;
        foreach (GridViewRow row in GridView1.Rows)
        {
            //Label lbInvoice = (Label)row.FindControl("lblInvoice");
            string strInvoice = GridView1.DataKeys[row.RowIndex].Values["lblInvoice"].ToString();
            objinvoice.Invoice = strInvoice;
            RadioButton rdn = (RadioButton)row.FindControl("rdnRenew");
            if (rdn.Checked)
            {
                actkey = oCustomerDetails.CreateRandom(20);
                objinvoice.activationcode = actkey;
                objinvoice.editInvoice(actkey, strInvoice);
            }
        }
    

    但我在这个问题上犯了一个错误

    string strInvoice = GridView1.DataKeys[row.RowIndex].Values["lblInvoice"].ToString();
    

    我写的东西有什么问题吗

    我声明我的项目模板如下

          <ItemTemplate>
                            <asp:Label ID="lblInvoice" runat="server" Text='<%# Eval("invoceNo") %>'></asp:Label>
                        </ItemTemplate>
    
    3 回复  |  直到 12 年前
        1
  •  2
  •   Soner Gönül to StackOverflow    14 年前
    <asp:Label ID="lblInvoice" runat="server" Text=<%# Eval("invoceNo") %>
    

    您收到此错误,因为可能是此行没有将任何值设置为 label .

     string strInvoice = GridView1.DataKeys[row.RowIndex].Values["lblInvoice"].ToString();
    

    如果值为空,则 remove(.ToString()) 可能会有用

        2
  •  0
  •   Developer    14 年前

    这是有效的

     string strInvoice = GridView1.DataKeys[row.RowIndex].Values["invoceNo"].ToString();
    

    对于网格,我们还必须分配数据键值…

        3
  •  0
  •   Echilon Mafarnakus    12 年前

    得到答案忘了给出 datakeynames 在里面 GridView .