日期:2014-05-18  浏览次数:21590 次

DataGridview中CheckBox如何赋值
VS 2005.

我一个DataGridView,其中第三列是CheckBox,只是CheckBox,没绑定数据..!!!
 启动这个版面的时候,我希望它的第二行第三列的这个CheckBox是选中状态....
 请问这个该怎么实现。,
该写在哪个事件里面....

------解决方案--------------------
DataGridView.Rows[1].Cell[2].Select.. = true;只记得这个样子
忘记了,不好意思
------解决方案--------------------
探讨
DataGridView.Rows[1].Cell[2].Select.. = true;只记得这个样子
忘记了,不好意思

------解决方案--------------------
1楼那个就可以了。
------解决方案--------------------
放在rowdatabind事件里(事件名可能不准确)
也可以在数据绑定后直接写
GridView1.DataBind();
CheckBox cb = GridView1.RowsRows[1].FindControl("CheckBox1") as CheckBox;
if(cb!=null)
cb.Checked;
------解决方案--------------------
转换为datagridviewcheckbox后再初始化
------解决方案--------------------
设为选中状态直接给该单元格的Value赋值就行了

C# code
if (dataGridView1.Rows.Count > 1 && dataGridView1.Columns.Count > 2)
{
    dataGridView1.Rows[1].Cells[0].Value = true;
}

------解决方案--------------------
this.dataGridView1.Rows[rowIndex].Cells[colName].Value = 1;

DataGridViewCheckBoxCell cbx = (DataGridViewCheckBoxCell)DataGridView1.Rows[1].Cells[0];
if ((bool)cbx.FormattedValue)
 {

 }
 
------解决方案--------------------
DataGridView.Rows[1].Cell[2].SelectItem = true