这是一个场景。
我有checkbox(名称:“全部检查”ID:chkItems)和datagridview。当我单击此复选框时,datagridview上的所有复选框也将被选中。
我还在网格上添加了复选框列。
DataGridViewCheckBoxColumn CheckboxColumn = new DataGridViewCheckBoxColumn();
CheckBox chk = new CheckBox();
CheckboxColumn.Width = 20;
GridView1.Columns.Add(CheckboxColumn);这是checkbox后面的代码。row.Cell上出现问题
private void chkItems_CheckedChanged(object sender, EventArgs e)
{
foreach (DataGridViewRow row in GridView1.Rows)
{
DataGridViewCheckBoxCell chk = e.row.Cells(0);
if (chk.Selected == false)
{
row.Cells(0).Value = true;
}
}
} 发布于 2012-11-06 09:00:35
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell) row.Cells[0];而不是
DataGridViewCheckBoxCell chk = e.row.Cell(0);*EDIT:*我认为你真的想这么做:
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell) row.Cells[0];
chk.Value = !(chk.Value == null ? false : (bool) chk.Value); //because chk.Value is initialy null
}发布于 2016-09-29 11:40:39
private void setCheckBoxInDataGrid(DataGridView dgv, int pos, bool isChecked)
{
for (int i = 0; i < dgv.RowCount; i++)
{
dgv.Rows[i].DataGridView[pos, i].Value = isChecked;
}
}我就是这么做的
发布于 2017-06-28 09:20:31
试试这个
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
row.Cells[0].Value = row.Cells[0].Value == false ? true : false;
}https://stackoverflow.com/questions/13242861
复制相似问题