在datagridview中添加了一列DataGridViewCheckBoxColumn,给其赋值为true,dataGridView1.Rows[i].Cells[0].Value = true,可是有时并没有出现小勾(多行的checkbox,有一个被单击过,再来遍历时,这个曾被单击过的checkbox就不会正确的显示或取消小勾),可能是相当于checkbox的Checked属性没有同时赋值成功。
    DataGridViewCheckBoxColumn是否也有个类似checked的属性呢?或者怎么保证为其赋值时,能相应的出现或取消小勾?

解决方案 »

  1.   

    不知道你到底操作的啥,反正dataGridView1.Rows[i].Cells[0].Value = true是没问题的
      

  2.   

    将列表checkbox列的FalseValue=false;TrueValue=true;
      

  3.   


     private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e) 
       { 
         if (this .dataGridView1.IsCurrentCellDirty)
             { 
             this .dataGridView1.CommitEdit(DataGridViewDataErrorContexts .Commit); 
             } 
       }
    加这个事件试试
      

  4.   

    [Quote=引用 4 楼  的回复:]我是想通过单击表头实现 DataGridViewCheckBoxColumn的全选或全不选,当有至少一个checkbox未被选中时,则全部赋值为选中;如原本就是全选中状态,则全部赋值为不选中。
    但由于第一行始终是选择状态(蓝色选中状态,但是checkbox并没有勾上),所以对第一行无效。即使给其赋值为true,也没有出现勾,怎么办?private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
            {
                if (e.RowIndex == -1 && e.ColumnIndex == -1)//单击表头
                {
                    bool checkAll = false;
                    for (int i = 0; i < dataGridView1.Rows.Count; i++)
                    {
                        if (dataGridView1.Rows[i].Cells[0].Value == null)//没有给checkBox列激活即选中的情况下,直接获取值.ToString会报错
                        {
                            checkAll = false;//checkBox列还未激活,即都还为选中
                            break;
                        }
                        else if (dataGridView1.Rows[i].Cells[0].Value.ToString() == "True")
                        {
                            string a = dataGridView1.Rows[i].Cells[0].Value.ToString();
                            checkAll = true;
                        }
                        else if (dataGridView1.Rows[i].Cells[0].Value.ToString() != "True")
                        {
                            checkAll = false;//有未被选中的记录,则标记全选为false,跳出循环
                            break;
                        }                }
                    if (checkAll == false) //存在未选的记录,则更新为全选
                    {
                        for (int i = 0; i < dataGridView1.Rows.Count; i++)
                        {
                            dataGridView1.Rows[i].Cells[0].Value = true;
                        }
                    }
                    else //如果原为全选状态,则更新为全不选
                    {
                        for (int i = 0; i < dataGridView1.Rows.Count; i++)
                        {
                            dataGridView1.Rows[i].Cells[0].Value = false;
                        }
                        //下面的代码都无法彻底清空行选中状态(不是勾上)
                        dataGridView1.ClearSelection();
                        dataGridView1.CurrentCell = null;
                     
                        // dataGridView1.Rows[0].Selected = false;
                    }
                }
            }
      

  5.   

    我是想通过单击表头实现 DataGridViewCheckBoxColumn的全选或全不选,当有至少一个checkbox未被选中时,则全部赋值为选中;如原本就是全选中状态,则全部赋值为不选中。
    但由于第一行始终是选择状态(蓝色选中状态,但是checkbox并没有勾上),所以对第一行无效。即使给其赋值为true,也没有出现勾,怎么办?private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
            {
                if (e.RowIndex == -1 && e.ColumnIndex == -1)//单击表头
                {
                    bool checkAll = false;
                    for (int i = 0; i < dataGridView1.Rows.Count; i++)
                    {
                        if (dataGridView1.Rows[i].Cells[0].Value == null)//没有给checkBox列激活即选中的情况下,直接获取值.ToString会报错
                        {
                            checkAll = false;//checkBox列还未激活,即都还为选中
                            break;
                        }
                        else if (dataGridView1.Rows[i].Cells[0].Value.ToString() == "True")
                        {
                            string a = dataGridView1.Rows[i].Cells[0].Value.ToString();
                            checkAll = true;
                        }
                        else if (dataGridView1.Rows[i].Cells[0].Value.ToString() != "True")
                        {
                            checkAll = false;//有未被选中的记录,则标记全选为false,跳出循环
                            break;
                        }                }
                    if (checkAll == false) //存在未选的记录,则更新为全选
                    {
                        for (int i = 0; i < dataGridView1.Rows.Count; i++)
                        {
                            dataGridView1.Rows[i].Cells[0].Value = true;
                        }
                    }
                    else //如果原为全选状态,则更新为全不选
                    {
                        for (int i = 0; i < dataGridView1.Rows.Count; i++)
                        {
                            dataGridView1.Rows[i].Cells[0].Value = false;
                        }
                        //下面的代码都无法彻底清空行选中状态(不是勾上)
                        dataGridView1.ClearSelection();
                        dataGridView1.CurrentCell = null;
                     
                        // dataGridView1.Rows[0].Selected = false;
                    }
                }
            }
      

  6.   

    我是想通过单击表头实现 DataGridViewCheckBoxColumn的全选或全不选,当有至少一个checkbox未被选中时,则全部赋值为选中;如原本就是全选中状态,则全部赋值为不选中。
    但由于第一行始终是选择状态(蓝色选中状态,但是checkbox并没有勾上),所以对第一行无效。即使给其赋值为true,也没有出现勾,怎么办?private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
            {
                if (e.RowIndex == -1 && e.ColumnIndex == -1)//单击表头
                {
                    bool checkAll = false;
                    for (int i = 0; i < dataGridView1.Rows.Count; i++)
                    {
                        if (dataGridView1.Rows[i].Cells[0].Value == null)//没有给checkBox列激活即选中的情况下,直接获取值.ToString会报错
                        {
                            checkAll = false;//checkBox列还未激活,即都还为选中
                            break;
                        }
                        else if (dataGridView1.Rows[i].Cells[0].Value.ToString() == "True")
                        {
                            string a = dataGridView1.Rows[i].Cells[0].Value.ToString();
                            checkAll = true;
                        }
                        else if (dataGridView1.Rows[i].Cells[0].Value.ToString() != "True")
                        {
                            checkAll = false;//有未被选中的记录,则标记全选为false,跳出循环
                            break;
                        }                }
                    if (checkAll == false) //存在未选的记录,则更新为全选
                    {
                        for (int i = 0; i < dataGridView1.Rows.Count; i++)
                        {
                            dataGridView1.Rows[i].Cells[0].Value = true;
                        }
                    }
                    else //如果原为全选状态,则更新为全不选
                    {
                        for (int i = 0; i < dataGridView1.Rows.Count; i++)
                        {
                            dataGridView1.Rows[i].Cells[0].Value = false;
                        }
                        //下面的代码都无法彻底清空行选中状态(不是勾上)
                        dataGridView1.ClearSelection();
                        dataGridView1.CurrentCell = null;
                     
                        // dataGridView1.Rows[0].Selected = false;
                    }
                }
            }
      

  7.   

    第一行始终是选择状态(蓝色选中状态,但是checkbox并没有勾上),所以对第一行无效
    dataGridView1.Rows[i].Cells[0].Value = true;这个复值肯定和是不是选不选中状态没关系的   
      

  8.   

    换个事件试试   试试  cellclick事件  或者  cellcontentclick
      

  9.   

    dataGridView1.Rows[i].Cells[0].Value = true
      

  10.   


    用cellclick或cellcontentclick事件,则不能选中表头了
    e.RowIndex == -1 && e.ColumnIndex == -1
      

  11.   

    总算基本解决了这个问题:
    this.dataGridView1.EndEdit();//结束编辑状态,必须要此句
    this.dataGridView1.Rows[i].Cells[0].Value = true;
    在赋值之前加上这一句,就搞定了。
    当然在更新为全不选时,以下两句都要:                   
     dataGridView1.ClearSelection();
     dataGridView1.CurrentCell = null;
      

  12.   

    哦,另外,datagridview的checkbox列只是单击它,出现小勾,或取消小勾,该单元格的Value不一定相应的是true或者false,所以我还加上了以下的代码。            if (e.ColumnIndex == 0 && e.RowIndex != -1)
                {
                    int rIndex = dataGridView1.CurrentCell.RowIndex;
                    if (this.dataGridView1.Rows[rIndex].Cells[0].EditedFormattedValue.ToString() == "True")//或者=="False"
                    {
                        this.dataGridView1.Rows[rIndex].Cells[0].Value = false;
                    }
                    else
                    {
                        this.dataGridView1.Rows[rIndex].Cells[0].Value = true;
                    }
                }