2012-10-02 24 views
0

我有以下aspx代碼;在gridview中獲取datarow vales錯誤

<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 

的代碼?插入功能工作方式相同,工作正常。 未將對象引用設置爲對象的實例。

回答

0

我認爲這個問題是您正在尋找在頁腳標籤lblEditId控制:

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

試試這個:

更新您刪除的LinkBut​​ton到(添加ID到CommandArgument):

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

接着,以該更換您的**代碼: 注:根據您所提供的代碼,它看起來像lblEditId是在第2列,因此與c ell(1) - 基於零的索引,而不是單元格(3)。

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