关于GridView控件的DeleteRow()方法的问题,删除不成功
我有个批量删除的功能,但是只能删除一个,比如我选序号为010203点批量删除后之删除01也就是说只删除第一个勾选的不能把这三个全删除这是怎么回事啊?请高手指点啊,下边是原...
我有个批量删除的功能,但是只能删除一个,比如我选序号为 01 02 03 点批量删除后之删除01 也就是说只删除第一个勾选的 不能把这三个全删除 这是怎么回事啊?请高手指点啊,下边是原代码,到底哪里出问题了?
这是后台代码:
protected void delselect_Click(object sender, EventArgs e)
{
int i = 0;
while (i < GridView1.Rows.Count)
{
if (GridView1.Rows[i].RowType == DataControlRowType.DataRow)
{
CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (cb.Checked == true)
{
GridView1.DeleteRow(i);
}
}
i = i + 1;
}
}
protected void selectall_Click(object sender, EventArgs e)
{
Button tb = (Button)GridView1.BottomPagerRow.FindControl("selectall");
//Response.Write(tb.Text);
//return;
bool a;
if (tb.Text == "全部选中")
{
tb.Text = "取消全部选中";
a = true;
}
else
{
tb.Text = "全部选中";
a = false;
}
int i = 0;
while (i < GridView1.Rows.Count)
{
if (GridView1.Rows[i].RowType == DataControlRowType.DataRow)
{
CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
cb.Checked = a;
}
i = i + 1;
}
前台页面代码:
<div style="text-align:left;">
<asp:Button ID="selectall" runat="server" Text="全部选中" OnClick="selectall_Click" CausesValidation="False" /> <asp:Button ID="delselect"
runat="server" Text="删除选中的记录" OnClick="delselect_Click" CausesValidation="False" />
</div>
能不能具体说下啊. 展开
这是后台代码:
protected void delselect_Click(object sender, EventArgs e)
{
int i = 0;
while (i < GridView1.Rows.Count)
{
if (GridView1.Rows[i].RowType == DataControlRowType.DataRow)
{
CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (cb.Checked == true)
{
GridView1.DeleteRow(i);
}
}
i = i + 1;
}
}
protected void selectall_Click(object sender, EventArgs e)
{
Button tb = (Button)GridView1.BottomPagerRow.FindControl("selectall");
//Response.Write(tb.Text);
//return;
bool a;
if (tb.Text == "全部选中")
{
tb.Text = "取消全部选中";
a = true;
}
else
{
tb.Text = "全部选中";
a = false;
}
int i = 0;
while (i < GridView1.Rows.Count)
{
if (GridView1.Rows[i].RowType == DataControlRowType.DataRow)
{
CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
cb.Checked = a;
}
i = i + 1;
}
前台页面代码:
<div style="text-align:left;">
<asp:Button ID="selectall" runat="server" Text="全部选中" OnClick="selectall_Click" CausesValidation="False" /> <asp:Button ID="delselect"
runat="server" Text="删除选中的记录" OnClick="delselect_Click" CausesValidation="False" />
</div>
能不能具体说下啊. 展开
3个回答
展开全部
循环有问题,自己断点调试。
protected void btnDelete_Click(object sender, EventArgs e)
{
int ProcessSuccess=0;
int ProcessFail = 0;
string AlertMsg = string.Empty;
foreach (GridViewRow gvwr in gvwPerDetail.Rows)
{
if (((CheckBox)gvwr.FindControl("Id")).Checked == true)
{
int ID = Convert.ToInt32(gvwPerDetail.DataKeys[gvwr.RowIndex].Value.ToString());
bool blReturn=bllEntPersonnel.DeleteReturn(ID);
if (blReturn)
ProcessSuccess++;
else21.
ProcessFail++;
}
}
BindGrid();
if(ProcessFail==0)
AlertMsg = "成功删除了" + ProcessSuccess.ToString() + "条记录!";
else28. AlertMsg = "共选择" + Convert.ToString(ProcessSuccess + ProcessFail) + "条记录,\\n成功删除了" +ProcessSuccess.ToString() + "条,\\n" + ProcessFail.ToString() + "条记录因有相关信息,无法删除!";
ScriptUtil.Alert(AlertMsg);
}
首先你既然从0开始 GridView1.Rows.Count 数据行是要-1才能匹配
if (cb.Checked == true)
{
GridView1.DeleteRow(i);
}
这里也有问题,你删除指向的应该是gridview的索引,或者是绑定数据列的ID 而不是i ,i知识你定义的一个变量
protected void btnDelete_Click(object sender, EventArgs e)
{
int ProcessSuccess=0;
int ProcessFail = 0;
string AlertMsg = string.Empty;
foreach (GridViewRow gvwr in gvwPerDetail.Rows)
{
if (((CheckBox)gvwr.FindControl("Id")).Checked == true)
{
int ID = Convert.ToInt32(gvwPerDetail.DataKeys[gvwr.RowIndex].Value.ToString());
bool blReturn=bllEntPersonnel.DeleteReturn(ID);
if (blReturn)
ProcessSuccess++;
else21.
ProcessFail++;
}
}
BindGrid();
if(ProcessFail==0)
AlertMsg = "成功删除了" + ProcessSuccess.ToString() + "条记录!";
else28. AlertMsg = "共选择" + Convert.ToString(ProcessSuccess + ProcessFail) + "条记录,\\n成功删除了" +ProcessSuccess.ToString() + "条,\\n" + ProcessFail.ToString() + "条记录因有相关信息,无法删除!";
ScriptUtil.Alert(AlertMsg);
}
首先你既然从0开始 GridView1.Rows.Count 数据行是要-1才能匹配
if (cb.Checked == true)
{
GridView1.DeleteRow(i);
}
这里也有问题,你删除指向的应该是gridview的索引,或者是绑定数据列的ID 而不是i ,i知识你定义的一个变量
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐于2016-01-12 · 知道合伙人数码行家
关注
展开全部
GridView控件删除DeleteRow()方法:
<%@ Page language="C#" %>
<script runat="server">
void UpdateRowButton_Click(Object sender, EventArgs e)
{
// Programmatically update the current record in edit mode.
CustomersGridView.UpdateRow(CustomersGridView.EditIndex, true);
}
void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// Enable the UpdateRowButton button only when the GridView control
// is in edit mode.
switch (e.CommandName)
{
case "Edit":
UpdateRowButton.Enabled = true;
break;
case "Cancel":
UpdateRowButton.Enabled = false;
break;
case "Update":
UpdateRowButton.Enabled = false;
break;
default:
UpdateRowButton.Enabled = false;
break;
}
}
</script>
<html>
<body>
<form runat="server">
<h3>GridView UpdateRow Example</h3>
<asp:button id="UpdateRowButton"
text="Update Record"
enabled="false"
onclick="UpdateRowButton_Click"
runat="server"/>
<hr/>
<!-- The GridView control automatically sets the columns -->
<!-- specified in the datakeynames property as read-only. -->
<!-- No input controls are rendered for these columns in -->
<!-- edit mode. -->
<asp:gridview id="CustomersGridView"
allowpaging="true"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="true"
autogenerateeditbutton="true"
datakeynames="CustomerID"
onrowcommand="CustomersGridView_RowCommand"
runat="server">
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="CustomersSqlDataSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
<%@ Page language="C#" %>
<script runat="server">
void UpdateRowButton_Click(Object sender, EventArgs e)
{
// Programmatically update the current record in edit mode.
CustomersGridView.UpdateRow(CustomersGridView.EditIndex, true);
}
void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// Enable the UpdateRowButton button only when the GridView control
// is in edit mode.
switch (e.CommandName)
{
case "Edit":
UpdateRowButton.Enabled = true;
break;
case "Cancel":
UpdateRowButton.Enabled = false;
break;
case "Update":
UpdateRowButton.Enabled = false;
break;
default:
UpdateRowButton.Enabled = false;
break;
}
}
</script>
<html>
<body>
<form runat="server">
<h3>GridView UpdateRow Example</h3>
<asp:button id="UpdateRowButton"
text="Update Record"
enabled="false"
onclick="UpdateRowButton_Click"
runat="server"/>
<hr/>
<!-- The GridView control automatically sets the columns -->
<!-- specified in the datakeynames property as read-only. -->
<!-- No input controls are rendered for these columns in -->
<!-- edit mode. -->
<asp:gridview id="CustomersGridView"
allowpaging="true"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="true"
autogenerateeditbutton="true"
datakeynames="CustomerID"
onrowcommand="CustomersGridView_RowCommand"
runat="server">
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="CustomersSqlDataSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你把循环改成fro循环或foreach 试一下
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询