gridview中出错

时间:2010-08-14 08:32:04

标签: c# asp.net gridview

我在按钮下编写了我的代码,如下所示

 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 个答案:

答案 0 :(得分:2)

<asp:Label ID="lblInvoice" runat="server" Text=<%# Eval("invoceNo") %>

您收到此错误,因为此行可能未将任何值设置为label

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

如果value为null,那么remove(.ToString())可能会起作用

答案 1 :(得分:0)

得到答案忘记在datakeynames中提供GridView

答案 2 :(得分:0)

这是工作中的

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

并且对于网格我们必须分配Datakey值...