mei*_*rav 7 c# checkbox datagridview winforms
我在Winform中有一个dataGridView,我在datagrid中添加了一个带有复选框的列,使用了我在这里看到的代码:
DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
{
column.HeaderText = "Export";
column.Name = "Export";
column.AutoSizeMode =
DataGridViewAutoSizeColumnMode.DisplayedCells;
column.FlatStyle = FlatStyle.Standard;
column.CellTemplate = new DataGridViewCheckBoxCell(false);
column.CellTemplate.Style.BackColor = Color.White;
}
gStudyTable.Columns.Insert(0, column);
Run Code Online (Sandbox Code Playgroud)
这工作但我希望检查checkBox作为默认值我添加:
foreach (DataGridViewRow row in gStudyTable.Rows)
{
row.Cells[0].Value = true;
}
Run Code Online (Sandbox Code Playgroud)
但复选框col仍然未选中.我正在使用一个集合作为我的数据源,并在添加数据源后更改了col的值.
Car*_*ras 10
我认为没有办法在列声明上设置选中的值.在设置datasource之后,您必须检查行检查它(例如在DataBindingComplete事件中):
for (int i = 0; i < dataGridView1.Rows.Count -1; i++)
{
dataGridView1.Rows[i].Cells[0].Value = true;
}
Run Code Online (Sandbox Code Playgroud)
使用您的列名称:
for (int i = 0; i < dataGridView1.Rows.Count -1; i++)
{
dataGridView1.Rows[i].Cells["Export"].Value = true;
}
Run Code Online (Sandbox Code Playgroud)