datagridview中未选中复选框

本文关键字:复选框 datagridview | 更新日期: 2023-09-27 18:17:27

我动态设置DataGridView中少数行CheckBox列为"checked on"

我已经尝试了很多事情。下面是示例代码。:

DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dataGridRow.Cells["colChk"];
chk.Selected = true;
chk.Value = 1;

datagridview中未选中复选框

我已经测试了我的案例,它运行良好。

private void AddRow_Click(object sender, EventArgs e)
{
    dataGridView1.Rows.Add();
}
private void ChangeCheck_Click(object sender, EventArgs e)
{
    foreach (DataGridViewRow row in dataGridView1.SelectedRows)
    {
        if (row.Cells[0].ValueType == typeof(bool))
        {
            if (row.Cells[0].Value == null)
                row.Cells[0].Value = true;
            else
                row.Cells[0].Value = !(bool)row.Cells[0].Value;
        }
    }
}

如果DataGridView绑定数据源,我建议你只能改变你的属性从true->false,如果你需要更多的帮助,请评论我的帖子。