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();
  }
}

解决方案 »

  1.   

    你的foreach(DataGridItem MyDataGridItem in DataGrid1.Items)  
    写成foreach(Control c in this.Controls)  
        {
      

  2.   

    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++)
    {
      

  3.   

    google: girdview 72般绝技
      

  4.   


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

  5.   

    如果 你只有一列的话   MyDataGridItem.Cells[0]
      

  6.   


    不循环DataGrid1的列  我怎么更新数据库??
      

  7.   

    DataGrid1.Items[i].Cells[1].Text 
    =>
    MyDataGridItem.Cells[i].Text
      

  8.   

    你要得到的究竟是什么?
    是DataGrid1的当前循环到的行的其它信息吗?
    你的foreach的DataGridItem这个不就是你当前的行吗,
    你要的数据都可以去DataGridItem中找到的,不需要再去循环DataGrid1的列了,
    不然 你的foreach就完全没用了。