当编辑操作同时发生时,在 ASP.NET 中的 GridView 控件中执行删除操作
目前我正在使用这是在 GridView 控件中执行编辑的标准方法。但这允许我一次只能执行一项操作。在我的应用程序中,当一个字段的一个值发生更改时,当用户单击“更新”时。应更新行,并且还应触发一个删除查询。
我尝试使用手动编辑
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID"
DataSourceID="sdsample1" Visible="False" OnRowEditing="GridView1_OnRowEditing" OnRowUpdated="GridView1_OnRowUpdated">
- 想要获取后端当前编辑行的 CheckboxField 值及其 gridview 中的第 8 列
<asp:CheckBoxField DataField="Goal_Type" HeaderText="Goal_Type"
SortExpression="Goal_Type" />
protected void GridView1_OnRowEditing(object sender, GridViewEditEventArgs e)
{
((CheckBox)GridView1.Rows[GridView1.SelectedIndex].FindControl("Goal_Type"));
CheckBox chk2 = ((CheckBox)GridView1.Rows[GridView1.EditIndex].Cells[7].Controls[0]);
goal_type =Convert.ToString(chk2.Checked);
if(goal_type.Equals("False"))
Goal_flag.Value ="0";
else
Goal_flag.Value = "1";
}
然后一旦我获取复选框字段的值设置一些标志变量,我就在中执行删除操作下面的函数
protected void GridView1_OnRowUpdated(object sender, GridViewUpdatedEventArgs e)
{
CheckBox chk2 = ((CheckBox)GridView1.Rows[e.AffectedRows].Cells[7].Controls[0]);
Boolean goal_type = chk2.Checked;
if (goal_type == true && Goal_flag.Value.Equals("0"))
{
string connectionString =
WebConfigurationManager.ConnectionStrings["DataCollectionConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd;
int ID = Convert.ToInt32(((TextBox)GridView1.Rows[e.AffectedRows].FindControl("ID")).Text);
cmd =new SqlCommand("Delete from XXX WHERE (ID = " + ID + ") ", con);
cmd.CommandType =
CommandType.Text;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
我在“GridView1_OnRowEditing”函数本身收到此错误 指数超出范围。必须为非负数且小于集合的大小。 参数名称:index
请让我知道我在 GridView 中执行同时操作的方法是否正确。
Currently I am using the which is the standard way to perform Editing in a GridView control. But this allows me to perform only one operation at a time.In my application when one value of one field changes , when user clicks on "Update". Rows should be updated and also one delete query should be fired.
I tried using the manually editing
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID"
DataSourceID="sdsample1" Visible="False" OnRowEditing="GridView1_OnRowEditing" OnRowUpdated="GridView1_OnRowUpdated">
-- Want to get the CheckboxField value of the current edit row in the backnd and its the 8 column in the gridview
<asp:CheckBoxField DataField="Goal_Type" HeaderText="Goal_Type"
SortExpression="Goal_Type" />
protected void GridView1_OnRowEditing(object sender, GridViewEditEventArgs e)
{
((CheckBox)GridView1.Rows[GridView1.SelectedIndex].FindControl("Goal_Type"));
CheckBox chk2 = ((CheckBox)GridView1.Rows[GridView1.EditIndex].Cells[7].Controls[0]);
goal_type =Convert.ToString(chk2.Checked);
if(goal_type.Equals("False"))
Goal_flag.Value ="0";
else
Goal_flag.Value = "1";
}
Then once I get the value of checkbox field set some flag variable , I perform a delete operation in the below function
protected void GridView1_OnRowUpdated(object sender, GridViewUpdatedEventArgs e)
{
CheckBox chk2 = ((CheckBox)GridView1.Rows[e.AffectedRows].Cells[7].Controls[0]);
Boolean goal_type = chk2.Checked;
if (goal_type == true && Goal_flag.Value.Equals("0"))
{
string connectionString =
WebConfigurationManager.ConnectionStrings["DataCollectionConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd;
int ID = Convert.ToInt32(((TextBox)GridView1.Rows[e.AffectedRows].FindControl("ID")).Text);
cmd =new SqlCommand("Delete from XXX WHERE (ID = " + ID + ") ", con);
cmd.CommandType =
CommandType.Text;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
I am getting this error at the "GridView1_OnRowEditing" function itself
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Please let me know if my approach is correct in doing the simultaneous operation in GridView.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 RowEditing 事件进行多次更新。正如此 问题 中提到的:
为了查找控件,我建议采用以下方法:本期提到:
http://www.devexpress.com/Support/Center/p/Q91970.aspx
或者您可以通过一个存储过程调用来进行更新和删除。
Use the RowEditing event for the multiple updates. As mentioned in this issue:
For finding the control I recommend the approach that is mentioned in this issue:
http://www.devexpress.com/Support/Center/p/Q91970.aspx
Or you could do the update and delete through one stored procedure call.