我照这里的一片文章写了用checkbox删除datagrid中的记录的一段程序,确老是报错,那位高手帮忙解决一下?

七七&小鱼 2004-05-06 09:04:54
private DataTable dt=new DataTable();
private DataSet ds = new DataSet();
private void Button1_Click(object sender, System.EventArgs e)
{int i=0;int ii;
CheckBox cb;
foreach(DataGridItem dgi in this.DataGrid1.Items)
{
cb = (CheckBox) dgi.Cells[0].Controls[1];
if(cb.Checked==true)
{
ii=(int)DataGrid1.DataKeys[i];
ds.Tables["clientt"].Rows.Remove(this.ds.Tables["clientt"].Rows.Find(ii));
}
i++;

}

string con=ConfigurationSettings.AppSettings["DSN"];
SqlConnection myConnection = new SqlConnection(con);
SqlDataAdapter myCommand = new SqlDataAdapter("select * from clientt", myConnection);
myCommand.Update(ds, "clientt");
myCommand.Fill(ds, "clientt");

DataGrid1.DataBind();
}


就是点一下button删除选中的记录,不知道这样对不对?
报错的是

索引超出范围。必须为非负值并小于集合大小。参数名: index
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。参数名: index

源错误:


行 103: if(cb.Checked==true)
行 104: {
行 105: ii=(int)DataGrid1.DataKeys[i];
行 106: ds.Tables["clientt"].Rows.Remove(this.ds.Tables["clientt"].Rows.Find(ii));
行 107: }

...全文
54 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
nnh 2004-05-07
  • 打赏
  • 举报
回复

Sub btnDel_OnClick(ByVal sender As Object, ByVal e As EventArgs)
Dim myDataGridItem_del As DataGridItem
Dim chkSelected_del As System.Web.UI.WebControls.CheckBox
Dim strBoardType_del As String
Dim strBoardID_del As String

lblSelect.Text = "<br>You selected the Following items:<br><br>"
lblStatus.Text=""
For Each myDataGridItem_del In MyDataGrid.Items
chkSelected_del = myDataGridItem_del.FindControl("chkSelection")
If chkSelected_del.Checked Then
lblSelect.visible=true
strBoardType_del= CType(myDataGridItem_del.FindControl("lblBoardType"), Label).Text
strBoardID_del = CType(myDataGridItem_del.FindControl("hdnBoardid"), Label).Text
'--------------------------------------------
Dim MyDelCommand As OleDbCommand
Dim DeleteCmd As String = "DELETE FROM [min] WHERE id = @Id"

MyDelCommand = New OleDbCommand(DeleteCmd, MyConnection)
MyDelCommand.Parameters.Add(New OleDbParameter("@Id", OleDbType.VarChar, 11))
MyDelCommand.Parameters("@Id").Value = int32.parse(strBoardID_del)

MyDelCommand.Connection.Open()
MyDelCommand.ExecuteNonQuery()
MyDelCommand.Connection.Close()
'---------------------------------------------
lblStatus.Text += "The id is Deleted<b>" & strBoardType_del & "</b>"
lblStatus.Text += " and the id is Deleted<b>" & strBoardID_del & "</b><br>"
else
lblSelect.visible=false
End If
Next
try
BindDataGrid()
catch
Mydatagrid.CurrentpageIndex=MyDataGrid.pagecount-1
BindDataGrid()
end try
End Sub
vzxq 2004-05-07
  • 打赏
  • 举报
回复
帮你UP
孟子E章 2004-05-07
  • 打赏
  • 举报
回复
http://dotnet.aspx.cc/ShowDetail.aspx?id=FF130C7F-3650-4DA6-8943-8AA4AF3E3459
七七&小鱼 2004-05-07
  • 打赏
  • 举报
回复
那我上面的代码应该怎么改啊?因为我港学这个,实在是不太懂
七七&小鱼 2004-05-07
  • 打赏
  • 举报
回复
问题解决了,谢谢各位!!
antony1118 2004-05-07
  • 打赏
  • 举报
回复
那可能就是1楼说得问题了
你去google搜索一下试试
或者看看这个
http://expert.csdn.net/Expert/topic/2995/2995178.xml?temp=.1162683
里面站点很多,你以后也用的到
七七&小鱼 2004-05-07
  • 打赏
  • 举报
回复
他说指定的转换无效


异常详细信息: System.InvalidCastException: 指定的转换无效。

源错误:


行 106: {
行 107:
行 108: cb = (CheckBox) dgi.Cells[0].Controls[0];
行 109:
行 110: if(cb.Checked==true

antony1118 2004-05-07
  • 打赏
  • 举报
回复
我刚装的系统,没装.net啊
感觉是index的问题
cell[0].controls[0]
试试
七七&小鱼 2004-05-07
  • 打赏
  • 举报
回复
还是不行啊
楼上的可以作个最简单的例子发给我吗?谢谢
antony1118 2004-05-07
  • 打赏
  • 举报
回复
会不会是这句错误
cb = (CheckBox) dgi.Cells[0].Controls[1];
cell[0]改成cell[1],controls里的index也改一下试试
七七&小鱼 2004-05-07
  • 打赏
  • 举报
回复
还是一样的错误啊
楼上的有这种简单点的例子吗?
可以发一个给我吗?
我搜索了这里的文章可是自己弄的时候就不会来了
antony1118 2004-05-07
  • 打赏
  • 举报
回复
把int i=0改成int i=dg.items.count-1
i++改成i--
试试
七七&小鱼 2004-05-07
  • 打赏
  • 举报
回复
没有人吗?救命啊!
七七&小鱼 2004-05-07
  • 打赏
  • 举报
回复
楼上的能不能提供一个c#的例子啊?或者旧在我得那个例子上改易下,我看不懂vb啊,谢了!
wd_318 2004-05-06
  • 打赏
  • 举报
回复
这里删除应该从后面开始删除,
比如你本来有10条记录,选择删除第1,2,10, 如果先删除了第1,2条记录,再删除第10条记录,那么因为此时总共只余8条记录了,第10条记录已经不存在,就出现了你那样的错误,
所以应该先删第10条记录,再删除第2条,再是第1条。。。。

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧