在C#中,我想让某个checkbox被选中,代码如下:datagridView1.Rows[i].cells[0].Value=true;可是这样无法实现。在网上看有这种方法:
DataGridViewCheckBoxCell dataCheck = (DataGridViewCheckBoxCell)datagridView1.Rows[i].Cells[0];
dataCheck.Value = true;可是也无法实现,请问是什么原因啊?

解决方案 »

  1.   

    不过 应该是datagridView1.Rows[i].cells[0].checked=true;吧?
      

  2.   

    check的值需要自定义true=?,false=?
    定义好了赋值会表现为选中或者不选中
    value并不等于true or false
      

  3.   

    直接把那个CheckBox的checked属性设为True不就行了吗
    Checked="True"
      

  4.   

    那一列是DataGridViewCheckBoxColumn吗?
      

  5.   

     private void button2_Click(object sender, EventArgs e)//全选/第1行        {
                for (int i = 0; i < dataGridView2.Rows.Count; i++)
                {
                    if ("False" == dataGridView2.Rows[i].Cells[0].FormattedValue.ToString())
                    {
                        dataGridView2.Rows[i].Cells[0].Value = true;
                    }
                    else if ("True" == dataGridView2.Rows[i].Cells[0].FormattedValue.ToString())
                    {
                        dataGridView2.Rows[i].Cells[0].Value = false;
                        dataGridView2.Rows[0].Cells[0].Value = true;
                        this.dataGridView2.VirtualMode = false;
                        
                    }
                }        } DataGridViewCheckBoxColumn newColumn = new DataGridViewCheckBoxColumn();
                     newColumn.HeaderText = "选择";
                     dataGridView2.Columns.Insert(0, newColumn);
                     dataGridView2.MultiSelect = true;
                     newColumn.InheritedStyle.Alignment = DataGridViewContentAlignment.TopCenter;
                     newColumn.Width = 35;
                     this.dataGridView2.VirtualMode = false;
    //第1列添加CHECKBOX
      

  6.   

     //全选
            private void btnSelectAll_Click(object sender, EventArgs e)
            {
                foreach (DataGridViewRow dr in dataGridView1.Rows)
                {
                    ((DataGridViewCheckBoxCell)dr.Cells[0]).Value = true;
                }
            }        //全不选
            private void btnNoSelect_Click(object sender, EventArgs e)
            {
                foreach (DataGridViewRow dr in dataGridView1.Rows)
                {
                    ((DataGridViewCheckBoxCell)dr.Cells[0]).Value = false;
                }
            }
      

  7.   

    datagridView1.Rows[i].cells[0].Value=true;
    ((DataGridViewCheckBoxCell)datagridView1.Rows[i].Cells[0]).Value = true;
    以上都是正解,只是刚开始的时候我把它们放到构造函数中了,所以没法实现。
    后来我把它们放到Load方法中就可以了,具体原理不明白,有知道的还不吝赐教!