DataGrid历遍CheckBox时出错!求高手指点!!

newboyjia 2011-07-07 10:04:12
foreach(DataGridItem MyDataGridItem in DataGrid1.Items)
{
string cards = "";

CheckBox chkbox=(CheckBox)MyDataGridItem.FindControl( "CheckBox1");

for (int i = 0; i <= DataGrid1.Columns.Count - 1; i++)
{
if (chkbox.Checked == true)
{
cards += "'" + DataGrid1.Items[i].Cells[1].Text + "',"; //这句出错!!索引超出范围。必须为非负值并小于集合大小。参数名: index

}
}
if (!cards.Equals(""))
{
cards = cards.Substring(0, cards.Length - 1);

SqlConnection cn = new SqlConnection(ConfigurationSettings.AppSettings["sbjx"]);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Update jyb set jylx=@jylx where id in (" + cards + ")";
cmd.Connection = cn;
cn.Open();

SqlParameter parm1 = new SqlParameter("@jylx", SqlDbType.Int);
parm1.Value = DropDownList1.SelectedItem.Text;

cmd.Parameters.Add(parm1);

cmd.ExecuteNonQuery();
cmd.Dispose();
cn.Close();
}
}
...全文
81 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
南瓜 2011-07-07
  • 打赏
  • 举报
回复
你要得到的究竟是什么?
是DataGrid1的当前循环到的行的其它信息吗?
你的foreach的DataGridItem这个不就是你当前的行吗,
你要的数据都可以去DataGridItem中找到的,不需要再去循环DataGrid1的列了,
不然 你的foreach就完全没用了。
xiaoyu821120 2011-07-07
  • 打赏
  • 举报
回复
DataGrid1.Items[i].Cells[1].Text
=>
MyDataGridItem.Cells[i].Text
newboyjia 2011-07-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 heroes12315 的回复:]
foreach(DataGridItem MyDataGridItem in DataGrid1.Items)
{
string cards = "";

CheckBox chkbox=(CheckBox)MyDataGridItem.FindControl( "CheckBox1");
//这一句取得就是每一个DataGridItem 中名为CheckBox1的CheckB……
[/Quote]

不循环DataGrid1的列 我怎么更新数据库??
ruanwei1987 2011-07-07
  • 打赏
  • 举报
回复
如果 你只有一列的话

MyDataGridItem.Cells[0]
ruanwei1987 2011-07-07
  • 打赏
  • 举报
回复

foreach (DataGridItem MyDataGridItem in DataGrid1.Items)
{
string cards = "";
CheckBox chkbox = (CheckBox)MyDataGridItem.FindControl("CheckBox1");
if (chkbox.Checked == true)
{
cards += "'" + MyDataGridItem.Cells[1].Text + "',";
}

}


这样写
moonwrite 2011-07-07
  • 打赏
  • 举报
回复
google: girdview 72般绝技
南瓜 2011-07-07
  • 打赏
  • 举报
回复
foreach(DataGridItem MyDataGridItem in DataGrid1.Items)
{
string cards = "";

CheckBox chkbox=(CheckBox)MyDataGridItem.FindControl( "CheckBox1");
//这一句取得就是每一个DataGridItem 中名为CheckBox1的CheckBox
//下面循环DataGrid1的列就没必要了吧!!

for (int i = 0; i <= DataGrid1.Columns.Count - 1; i++)
{
War3_Fan 2011-07-07
  • 打赏
  • 举报
回复
你的foreach(DataGridItem MyDataGridItem in DataGrid1.Items)
写成foreach(Control c in this.Controls)
{
newboyjia 2011-07-07
  • 打赏
  • 举报
回复
再顶~~~~~~
newboyjia 2011-07-07
  • 打赏
  • 举报
回复
··········

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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