gpt4 book ai didi

c# - 从 GridView 中删除记录时出错

转载 作者:太空宇宙 更新时间:2023-11-03 13:52:44 25 4
gpt4 key购买 nike

我有一个带有以下标记的 GridView 控件:

 <asp:GridView ID="gvGroups" runat="server" Width="100%" AutoGenerateColumns="False"
ShowFooter="True" BorderColor="White" BorderStyle="Ridge" CellSpacing="1" BorderWidth="2px"
BackColor="White" CellPadding="3" GridLines="None" Font-Names="Tahoma" Font-Size="11px"
DataKeyNames="GroupId" OnRowDeleting="gvGroups_RowDeleting">
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<RowStyle BackColor="#DEDFDE" ForeColor="Black" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
<Columns>
<asp:TemplateField HeaderText="Row">
<ItemTemplate>
<asp:Literal ID="litRowNumberNormal" runat="server"></asp:Literal>
</ItemTemplate>
<FooterTemplate>
<asp:Literal ID="litRowNumberFooter" runat="server"></asp:Literal>
</FooterTemplate>
<ItemStyle HorizontalAlign="Center" />
<FooterStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Title">
<ItemTemplate>
<%#Eval("Title")%>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddTitle" runat="Server" BorderStyle="Solid" BorderWidth="1px"
Font-Names="Tahoma" Font-Size="11px" BorderColor="Black" />
</FooterTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditTitle" Text='<%# Bind("Title") %>' runat="server" BorderStyle="Solid"
BorderWidth="1px" Font-Names="Tahoma" Font-Size="11px" BorderColor="Black" />
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" ButtonType="Button" UpdateText="Save" CancelText="Cancel"
EditText="Edit" HeaderText="Edit">
<FooterStyle BackColor="#669900" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5A49A7" HorizontalAlign="Center" />
<ItemStyle BackColor="#FFC080" HorizontalAlign="Center" />
</asp:CommandField>
<asp:TemplateField HeaderText="Delete">
<FooterTemplate>
<asp:Button CommandName="Delete" Text="Delete" ID="btnRemove" runat="server" BorderStyle="Solid"
BorderWidth="1px" BackColor="#FFC080" Font-Names="Tahoma" Font-Size="11px" />
</FooterTemplate>
<ItemTemplate>
<asp:CheckBox ID="ChkRemove" runat="server"></asp:CheckBox>
</ItemTemplate>
<ItemStyle BackColor="LightCoral" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5A49A7" HorizontalAlign="Center" />
<FooterStyle BackColor="#669900" HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
</asp:GridView>

这个网格的模型是一个组类列表。组类如下:

public class Group
{
public int GroupId {get; set; }
public string Title {get; set; }
}

GroupId 是我的表的主键。当我按下删除按钮时,出现以下错误:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

我的 RowDeleting 事件处理程序代码:

protected void gvGroups_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
CheckBox chkRemove;

List<int> ids = new List<int>();


foreach (GridViewRow gvRow in gvGroups.Rows)
{
chkRemove = (CheckBox)gvRow.FindControl("ChkRemove");
if (chkRemove.Checked)
{
ids.Add(Int32.Parse(gvGroups.DataKeys[gvRow.RowIndex].Value.ToString()));
}
}

if (ids.Any())
{
GroupService.DeleteGroupById(ids);
}

this.BindGroups();
}

最佳答案

我们可以做的另一项工作是将“删除”按钮的 CommandName 属性更改为“删除”以外的任何内容,并在 RowCommand 事件中处理它,“删除”命令是触发 GridView 控件的 RowDeleting 事件的默认命令名称。

关于c# - 从 GridView 中删除记录时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13225986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com