在gridview错误中获取datarow值

时间:2012-10-02 14:20:44

标签: asp.net gridview

我有以下aspx代码;

 <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ProductId"
DataSourceID="edsinventory" OnRowCommand="GridView1_RowCommand"
ShowFooter="true" CssClass="mGrid">

    <Columns>
      <asp:TemplateField ShowHeader="False">
        <EditItemTemplate>
            <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" 
                CommandName="Update" Text="Update"></asp:LinkButton>
            &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
                CommandName="Cancel" Text="Cancel"></asp:LinkButton>
        </EditItemTemplate>
        <FooterTemplate><asp:LinkButton ID="LinkButton3" runat="server" CommandName="Insert">Insert</asp:LinkButton></FooterTemplate>
        <ItemTemplate>
            <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
                CommandName="Edit" Text="Edit"></asp:LinkButton>
            &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
                CommandName="Delete" Text="Delete"></asp:LinkButton>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Id" InsertVisible="False" 
        SortExpression="Id">
        <EditItemTemplate>
            <asp:Label ID="lblEditId" runat="server" Text='<%# Eval("Id") %>'></asp:Label>
        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="lblId" runat="server" Text='<%# Bind("Id")%>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="ProductId" SortExpression="ProductId">
        <EditItemTemplate>
            <asp:TextBox ID="tbProdId" Width="50" runat="server" Text='<%# Bind("ProductId")%>'></asp:TextBox>
        </EditItemTemplate>
        <FooterTemplate>
            <asp:TextBox ID="tbInsertProdId" Width="50" runat="server"></asp:TextBox>
        </FooterTemplate>
        <ItemTemplate>
            <asp:Label ID="lblProdId"  runat="server" Text='<%# Bind("ProductId")%>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
    'shortened for brevity



    </Columns>
</asp:GridView>

这是背后的代码

 Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs)
    ' This just provides easier access to the Cells in the GridView
    Dim cells = GridView1.FooterRow.Cells
    Dim ctx As New teckEntities()
    Dim addInventory As New inventory()

    If e.CommandName = ("Insert") Then
        'Create new Inventory object
        addInventory.ProductId = Convert.ToInt32(DirectCast(cells(3).FindControl("tbInsertProdId"), TextBox).Text)
        addInventory.Quantity = Convert.ToInt32(DirectCast(cells(4).FindControl("tbInsertQuantity"), TextBox).Text)
        addInventory.Location = Convert.ToString(DirectCast(cells(1).FindControl("tbInsertLocation"), TextBox).Text)
        'attach the fields to the inventory context
        ' note: Id autoincrements and doesn't need to be set manually

        InsertInventoryItem(addInventory)
        'need to call a gridview refresh here
        GridView1.DataBind()
    ElseIf e.CommandName = "Delete" Then

        **addInventory.Id = Convert.ToInt32(DirectCast(cells(3).FindControl("lblEditId"), Label).Text)**

        DeleteInventoryItem(Convert.ToInt32(addInventory.Id))
        GridView1.DataBind()
    End If

End Sub

我在删除例程中遇到以下错误**,为什么?插入功能以相同的方式工作并正常工作。 对象引用未设置为对象的实例。

1 个答案:

答案 0 :(得分:0)

我认为问题是你在Footer控件中看到了标签lblEditId:

Dim cells = GridView1.FooterRow.Cells
...
addInventory.Id = Convert.ToInt32(DirectCast(cells(3).FindControl("lblEditId"), Label).Text)

请改为尝试:

更新您的删除链接按钮(将ID添加到CommandArgument):

<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete"  CommandArgument='<%#Eval("Id")%>' Text="Delete"></asp:LinkButton>

然后,用以下代码替换你的**代码: 注意:根据您提供的代码,似乎lblEditId位于第2列,因此Cell(1) - 基于零的索引,而不是Cell(3)。

addInventory.Id = Convert.ToInt32(DirectCast(GridView1.Rows(e.CommandArgument).Cells(1).FindControl("lblEditId"), Label).Text)