[英]Error in deleting record from GridView
I have a GridView control with the following markup: 我有一个带有以下标记的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>
model of this grid is a List of Group class. 该网格的模型是Group类的List。 Group class is as follows: 组类如下:
public class Group
{
public int GroupId {get; set; }
public string Title {get; set; }
}
GroupId is primary key of my table. GroupId是表的主键。 When I press Delete button, I get the following error: 当我按Delete按钮时,出现以下错误:
Index was out of range. 索引超出范围。 Must be non-negative and less than the size of the collection. 必须为非负数并且小于集合的大小。
Parameter name: index 参数名称:索引
my RowDeleting event handler codes: 我的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事件中对其进行处理,“ Delete”命令是用于触发GridView控件的RowDeleting事件的默认CommandName。
the code looks correct but the event chosen is not . 代码看起来正确,但是选择的事件不正确。 as @AVD mentioned RowDeleting event is for per row
situation where each row
has its own Delete button
: 正如@AVD提到的那样,RowDeleting事件适用于per row
情况,其中每row
都有自己的Delete button
:
Occurs when a row's Delete button is clicked, but before the GridView control deletes the row 单击某行的“删除”按钮时,但在GridView控件删除该行之前发生
all you need is to add a btnRemove_Click
event and put your code there . 您所需btnRemove_Click
只是添加一个btnRemove_Click
事件并将代码放在此处。
If you want to do the multiple delete add a button called multiple delete outside the gridview and handle the onclick event, in the event handler delete the number of items that are selected like below 如果要进行多次删除,请在gridview外部添加一个名为“多次删除”的按钮并处理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();
}
You can delete according to your primary key in the stored procedure Hope this helps :D 您可以根据存储过程中的主键删除,希望对您有所帮助:D
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.