我有一个带有以下标记的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>
此网格的模型是Group类的List。 小组课程如下:
public class Group
{
public int GroupId {get; set; }
public string Title {get; set; }
}
GroupId是我表的主键。 当我按下删除按钮时,出现以下错误:
指数超出范围。必须是非负数且小于集合的大小。
参数名称: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();
}
答案 0 :(得分:1)
我们可以做的另一项工作是将“删除”按钮的CommandName属性更改为“删除”以外的任何内容,并在RowCommand事件中处理它, “删除”命令是用于触发GridView控件的RowDeleting事件的默认CommandName。
答案 1 :(得分:0)
代码看起来正确但选择的事件不是。正如@AVD提到的RowDeleting事件适用于per row
情况,其中每个row
都有自己的Delete button
:
单击行的“删除”按钮但在GridView之前发生 control删除行
您只需要添加btnRemove_Click
事件并将代码放在那里。
答案 2 :(得分:0)
如果要进行多次删除,请在gridview外添加一个名为multiple delete的按钮并处理onclick事件,在事件处理程序中删除所选项目的数量,如下所示
public void DeleteEverything(object sender, EventArgs e)
{
// this function is to delete the selected items based on the checkbox
CheckBox chkAll = (CheckBox)GridView1.HeaderRow.FindControl("SelectAllCheck");
// to get the Checkbox status in the header rows
if (chkAll.Checked == true)
{
int i = 0;
foreach (GridViewRow gvRow in GridView1.Rows)//to get all rows in that particular page
{
string Delete = Convert.ToString(GridView1.Rows[i].Cells[3].Text);
//Cells[3] is the column to get one by one rows cells[3] columns where you should keep your primary keys and in visible state
Bal_add.Delete(Delete);
i++;
}
Response.Redirect("Information.aspx");
}
else
{
int j=0;
foreach (GridViewRow gvRow in GridView1.Rows)
{
CheckBox chkSel = (CheckBox)gvRow.FindControl("SelectCheck");
if (chkSel.Checked == true)
{
string Delete = Convert.ToString(GridView1.Rows[j].Cells[3].Text);
//Cells[3] is the column to get one by one rows cells[3] columns where you should keep your primary keys and in visible state
Delete(Delete);
}
j++;
}
Response.Redirect("Information.aspx");
}
}
public void Delete(string UserEmail)
{
obj_add = new add();
string QueryString;
QueryString = System.Configuration.ConfigurationManager.ConnectionStrings["Admin_raghuConnectionString1"].ToString();
obj_SqlConnection = new SqlConnection(QueryString);
obj_SqlCommand = new SqlCommand("usp_DeleteDataProcedure");
obj_SqlCommand.CommandType = CommandType.StoredProcedure;
obj_SqlConnection.Open();
obj_SqlCommand.Parameters.AddWithValue("@UserEmail", UserEmail);//here @UserName is the variable that we declare in the stored procedure
obj_SqlCommand.Connection = obj_SqlConnection;
SqlDataReader obj_result = null;
obj_SqlCommand.CommandText = "usp_DeleteDataProcedure";
obj_result = obj_SqlCommand.ExecuteReader();
obj_SqlConnection.Close();
}
您可以根据存储过程中的主键删除希望这会有所帮助:D