选中Datagridview复选框不起作用

时间:2017-03-30 07:33:00

标签: c# datagridview

我在datagridview中添加了复选框,并尝试将默认值设置为已选中,但显示复选框未选中,这是我的代码:

OracleDataAdapter da = new OracleDataAdapter(qry, myConnection);
OracleCommandBuilder cmdBuilder = new OracleCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds);
dgv.DataSource = ds.Tables[0];

DataGridViewCheckBoxColumn checkColumn = new DataGridViewCheckBoxColumn();
checkColumn.Name = "X";
checkColumn.HeaderText = "X";
checkColumn.Width = 50;
checkColumn.ReadOnly = false;

dgv.Columns.Insert(8, checkColumn);

foreach (DataGridViewRow row in dgv.Rows)
{
   DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[dgv.Columns.Count - 1];
   chk.Value = chk.TrueValue;
}

我也试过以下代码,但仍未显示为" CHECKED":

chk.Value = true;

请帮我解决这个问题,谢谢

1 个答案:

答案 0 :(得分:1)

DataGridViewCheckBoxCell.TrueValue的默认值为null。设置拥有列的TrueValue属性:

checkColumn.TrueValue = true;