我在DataGridView中添加一个DataGridViewCheckBoxColumn列,想用程序的方法改变datagridviewcheckboxcell的选中状态(打勾或取消打勾)。搞了好久,希望大家能帮忙。     列表名是readperIodCheckBox      for (int i = 0; i < bs_cust_info.Rows.Count-1; i++) 
                {
                    string a = bs_cust_info.Rows[i].Cells["readperIod"].Value.ToString();
                    if (a.Equals("1"))
                    {        //在这里改变状态,但是不行
                        bs_cust_info.Rows[i].Cells["readperIodCheckBox"].Value = true;
                    }
                    else 
                    {
                        bs_cust_info.Rows[i].Cells["readperIodCheckBox"].Value = false;
                    }
                    
                } 这样子也不行,帮帮忙吧!

解决方案 »

  1.   

    CheckBox(GridView.Rows[i].Cell[j].FindControl("CheckBox1")).Checked=true/false;
      

  2.   

    DataGridViewCheckBoxColumn newColumn = new DataGridViewCheckBoxColumn(); 
                    newColumn.TrueValue =1; 
                    newColumn.FalseValue =0; 
                    newColumn.IndeterminateValue =0; 
      

  3.   

       for (int i = 0; i < bs_cust_info.Rows.Count - 1; i++)
                {
                    string a = bs_cust_info.Rows[i].Cells["readperIod"].Value.ToString();
                    CheckBox _CheckBox =(CheckBox)bs_cust_info.Rows[i].FindControl("CheckBox1");
                    if (a.Equals("1"))
                    {
                        _CheckBox.Checked = true;
                  
                    }
                    else
                    {
                        _CheckBox.Checked = false;
                  
                    }            } 
      

  4.   

    断点看看bs_cust_info.Rows[i].Cells["readperIodCheckBox"] 是个什么对象
      

  5.   

    CheckBox(GridView.Rows[i].Cell[j].FindControl("CheckBox1")).Checked=true/false;
      

  6.   

    cells[index] 不用string类型用int吧。就是取对象问题吧。。弄个断点就好办 啦
      

  7.   

    你的 readperIod 这格中如没有控件,就应该
    bs_cust_info.Rows[i].Cells["readperIod"].Text 不是Value
      

  8.   

    直接绑定的一个checkBox列啊
    对象都能取到
    是DataGridViewCheckBoxCell类型的对象啊
      

  9.   

    DataGridViewCheckBoxColumn newColumn = (DataGridViewCheckBoxColumn)bs_cust_info.Rows[i].Cells["readperIodCheckBox"]; 
                    newColumn.TrueValue =1; 
                    newColumn.FalseValue =0; 
                    newColumn.IndeterminateValue =0; 
      

  10.   

    关键这个条件成立不成立啊readperIodCheckBoxColumn.TrueValue =1; 
    readperIodCheckBoxColumn.FalseValue =0; 
    readperIodCheckBoxColumn.IndeterminateValue =0; bs_cust_info.EndEdit();
    foreach (DataGridViewRow DVGR in bs_cust_info.Rows)
        DVGR.Cells["readperIodCheckBox"].Value = (bs_cust_info.Rows[i].Cells["readperIod"].Value.ToString()=="1" ? 1 : 0);
    bs_cust_info.EndEdit();
      

  11.   

    试试下面的代码如何。来自http://bingning.net/VB/SOURCE/datagridview/datagridviewcheckboxcolumn.html//CurrentCellDirtyStateChanged事件处理器
     private void DataGridView1_CurrentCellDirtyStateChanged(
         object sender, EventArgs e)
     {
         if (DataGridView1.CurrentCellAddress.X == 0 &&
             DataGridView1.IsCurrentCellDirty)
         {
             // Commit
             DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
         }
     } //CellValueChanged事件处理器
     private void DataGridView1_CellValueChanged(
         object sender, DataGridViewCellEventArgs e)
     {
         //检测是否是CheckBox列
         if (e.ColumnIndex == 0 &&
             DataGridView1.Columns[e.ColumnIndex].ValueType == typeof(bool))
         {
             MessageBox.Show(
                 string.Format("第{0}行CheckBox值改变为{1}。",
                 e.RowIndex,
                 DataGridView1[e.ColumnIndex, e.RowIndex].Value));
         }
     }
      

  12.   

    .net 72 般技巧!上面有很多实例,很实用找一下吧!
      

  13.   

    上面的各位:
    没必要非要使用微软提供的checkboxColumn吧。
    完全可以自己将checkbox嵌入在datagridview里面呀。
      

  14.   

    楼主是要实现 全选和反选功能吗.........
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Cells["Column1"].Value != null)
                {
                    continue;
                }
                if ((bool)dataGridView1.Rows[i].Cells["Column1"].Value)
                {
                    dataGridView1.Rows[i].Cells["Column1"].Value = false;
                }
                else
                {
                    dataGridView1.Rows[i].Cells["Column1"].Value = false;
                }
            }/*不知道行不行,我做过....忘记了....好像是这个样*/
      

  15.   


            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Cells["Column1"].Value == null) //是==
                {
                    continue;
                }
                if ((bool)dataGridView1.Rows[i].Cells["Column1"].Value)
                {
                    dataGridView1.Rows[i].Cells["Column1"].Value = false;
                }
                else
                {
                    dataGridView1.Rows[i].Cells["Column1"].Value = false;
                }
            }
      

  16.   


            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Cells["Column1"].Value == null)
                {
                    dataGridView1.Rows[i].Cells["Column1"].Value = true;
                }
                if ((bool)dataGridView1.Rows[i].Cells["Column1"].Value)
                {
                    dataGridView1.Rows[i].Cells["Column1"].Value = false;
                }
                else
                {
                    dataGridView1.Rows[i].Cells["Column1"].Value = true;
                }
            }
      

  17.   

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                if (e.ColumnIndex > 1 && e.ColumnIndex < 5)
                {
                    bool t = (bool)dataGridView1[e.ColumnIndex, e.RowIndex].Value;
                    t = !t;
                    dataGridView1[e.ColumnIndex, e.RowIndex].Value = t;
                    dataGridView1.CurrentCell = dataGridView1[0, e.RowIndex];
                    dataGridView1.CurrentCell = dataGridView1[e.ColumnIndex, e.RowIndex];
                }
            }
    参考
      

  18.   

    不好意思 开始没明白你的意思...现在明白了 readperIod列如果是1的话,checkBox就打勾,否则不打勾
            for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
            {
                if (dataGridView1.Rows[i].Cells["readperIod"].Value.ToString() == "1")
                {
                    dataGridView1.Rows[i].Cells["readperIodCheckBox"].Value = true;
                }
                else
                {
                    dataGridView1.Rows[i].Cells["readperIodCheckBox"].Value = false;
                }
            }/*我试过了 我的是可以的.........*/
      

  19.   

    用LZ的代码 我也是可以的不知道LZ 怎么搞的把 a 打个断点 调试下  看下a 是什么值或者 把 false 和 true 换下位置
      

  20.   

    调试过了啊  if(){}else{}两个表达式都进去了   
    但是状态还是未选中状态!
    非常的郁闷
      

  21.   

    dataGridView1.AutoGenerateColumns = false;  //加上这么一句看下
      

  22.   

    把选中代码放在Form_Load里就可以变成选中了状态了..
      

  23.   

    //单击单元格发生的事件
       private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
                for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                {
                    if (dataGridView1.Rows[i].Cells["test"].Selected)
                    {
                        dataGridView1.Rows[i].Cells["test"].Value = true;
                    }
                    else
                    {
                        dataGridView1.Rows[i].Cells["test"].Value = false;
                    }
                }
            }我试了可这样可以
      

  24.   

    各位大哥,看准楼主的题目内容,是datagridview不是datagrid,是winform不是webform。自己没试过的话就不要乱讲。
      

  25.   

    我也遇到了和楼主同样问题,今天早上正常调试通过了,后来添加了些代码就不能正常初始化checkBox了:
    private void SYS_WorkOrder_Load(object sender, EventArgs e)
    {
       dataGridView1.DataSource = dt;
       dataGridView1.Columns["ID"].Visible = true;
       dataGridView1.Columns["ModifyFlag"].Visible = true;
       dataGridView1.Columns["EmployeeName"].HeaderText = "姓名";
       DataGridViewCheckBoxColumn dgvCheckBox = new DataGridViewCheckBoxColumn();
       dgvCheckBox.HeaderText = "选择";
       dgvCheckBox.HeaderCell.Style.Alignment=DataGridViewContentAlignment.MiddleCenter;
       dataGridView1.Columns.Insert(1, dgvCheckBox);   for (int i = 0; i < dataGridView1.Rows.Count; i++)
       {
           dataGridView1.Rows[i].Cells[1].Value = true;
           string flag = dataGridView1.Rows[i].Cells["ModifyFlag"].Value.ToString();
           if (flag=="2")
           {
               dataGridView1.Rows[i].Cells[1].Value = true;
               dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red;
           }
           if(flag =="1")
               dataGridView1.Rows[i].Cells[1].Value = true;
       }
    }