从GridView中的“删除”行获取值时出错

时间:2014-02-15 17:34:57

标签: c# asp.net gridview

点击删除按钮

时出现此错误
System.ArgumentOutOfRangeException:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

GridView3_RowDeleting如下

protected void GridView3_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    Label1.Text = GridView3.DataKeys[e.RowIndex].Value.ToString();
}

如何访问该特定行的值?

<asp:GridView ID="GridView3" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" HorizontalAlign="Left" Width="16px" Height="5px" PageSize="5" OnRowDeleting="GridView3_RowDeleting" OnSelectedIndexChanged="GridView3_SelectedIndexChanged">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:BoundField DataField="DName" HeaderText="DName" SortExpression="DName" />
            <asp:BoundField DataField="bloodGroup" HeaderText="Group" SortExpression="bloodGroup" />
            <asp:BoundField DataField="contact_number" HeaderText="Contact#" SortExpression="contact_number" />
            <asp:BoundField DataField="dateDonated" HeaderText="dateDonated" SortExpression="dateDonated" />
            <asp:BoundField DataField="city" HeaderText="city" SortExpression="city" />
            <asp:BoundField DataField="arid_number" HeaderText="Arid#" SortExpression="arid_number" />
            <asp:CommandField ShowDeleteButton="True" />
        </Columns>
        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
        <SortedAscendingCellStyle BackColor="#FDF5AC" />
        <SortedAscendingHeaderStyle BackColor="#4D0000" />
        <SortedDescendingCellStyle BackColor="#FCF6C0" />
        <SortedDescendingHeaderStyle BackColor="#820000" />
    </asp:GridView>

1 个答案:

答案 0 :(得分:0)

您在Gridview中缺少* DataKeyNames * 属性。

<asp:GridView DataKeyNames ="yourID"---------------------------------------->

如果您使用“DName”作为<asp:GridView DataKeyNames="DName"之类的DataKeyNames,那么您将获得标签中删除行的列DName值

Label1.Text = GridView3.DataKeys[e.RowIndex].Value.ToString();

Label1.Text =(您要删除的行的DName值)

以下是一个有效的例子:可能有帮助

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID"
        DataSourceID="SqlDataSource1">
        <Columns>
            <asp:CommandField ShowDeleteButton="True" />
            <asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True" SortExpression="ProductID" />
            <asp:BoundField DataField="ProductDesc" HeaderText="ProductDesc" SortExpression="ProductDesc" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthWindConnectionString %>"
        SelectCommand="SELECT [ProductID], [ProductDesc] FROM [Product]" 
        DeleteCommand="DELETE FROM [Product] WHERE [ProductID] = @ProductID" >  
        <DeleteParameters>
            <asp:Parameter Name="ProductID" Type="Int32" />
        </DeleteParameters>        
    </asp:SqlDataSource>

此处ProductID是在DataKeyNames和删除记录中使用的uniqueKey,而在您的情况下是DID ...


以下应该是您的代码...享受

<asp:GridView DataKeyNames="DID" ID="GridView3" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" 
CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" HorizontalAlign="Left" Width="16px" 
Height="5px" PageSize="5" OnRowDeleting="GridView3_RowDeleting" OnSelectedIndexChanged="GridView3_SelectedIndexChanged">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
        <asp:BoundField DataField="DID" HeaderText="DID" SortExpression="DID" Visible="false" />
            <asp:BoundField DataField="DName" HeaderText="DName" SortExpression="DName" />
            <asp:BoundField DataField="bloodGroup" HeaderText="Group" SortExpression="bloodGroup" />
            <asp:BoundField DataField="contact_number" HeaderText="Contact#" SortExpression="contact_number" />
            <asp:BoundField DataField="dateDonated" HeaderText="dateDonated" SortExpression="dateDonated" />
            <asp:BoundField DataField="city" HeaderText="city" SortExpression="city" />
            <asp:BoundField DataField="arid_number" HeaderText="Arid#" SortExpression="arid_number" />
            <asp:CommandField ShowDeleteButton="True" />
        </Columns>
        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
        <SortedAscendingCellStyle BackColor="#FDF5AC" />
        <SortedAscendingHeaderStyle BackColor="#4D0000" />
        <SortedDescendingCellStyle BackColor="#FCF6C0" />
        <SortedDescendingHeaderStyle BackColor="#820000" />
    </asp:GridView>

    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnectionString %>" 
    DeleteCommand="DELETE FROM [tblDonors] WHERE [DID]=@DID" 
    SelectCommand="SELECT [DName], [gender], [bloodGroup], [contact_number], [city], [arid_number], [avallibilityTime], [dateDonated], [email] FROM [tblDonors]">
    <DeleteParameters>
            <asp:Parameter Name="DID" Type="Int32" />
        </DeleteParameters>  
    </asp:SqlDataSource>