C# 从';删除';在GridView中单击

C# 从';删除';在GridView中单击,c#,asp.net,gridview,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_行删除如下所示 protected void GridView3_RowDeleting(object sender, GridViewDeleteEvent

单击“删除”按钮时出现此错误

System.ArgumentOutOfRangeException:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
GridView3_行删除如下所示

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>

Gridview中缺少*DataKeyNames*属性

<asp:GridView DataKeyNames ="yourID"---------------------------------------->
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是uniqueKey,用于DataKeyNames和删除记录,而在您的例子中是


下面应该是你的代码…享受吧

<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>


我不太熟悉它们,但是
GridViewDeleteEventArgs
中是否包含您需要的值?我不是ASP.NET c#专家。我想获取单击“删除”按钮的那一行的值。每一行上都有删除按钮吗?您的标记是什么样子的?请共享您的gridView标记它是您的ID的“CommandField”?抱歉,我的知识有限。请使用“DName”或任何其他列名代替您的ID。抱歉,gridView标记中的不是DataKeys,而是DataKeyNames,我编辑了我的回答。数据源“SqlDataSource1”不支持删除错误,除非指定了DeleteCommand。连我都说了。DeleteCommand=“从[Test.dbo.tblUser]删除,其中[DID]=81”也共享您的数据源
<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>