关于DataGridView中DataGridViewCheckBoxCell 控制选中,以下是单选实现,需要设置该列readonly属性为ture,否则只有当选中其他时才会有反应!
以下部分代码
dgv.CommitEdit(DataGridViewDataErrorContexts.Commit);
DataGridViewCheckBoxCell chktmp = (DataGridViewCheckBoxCell)dgv.Rows[e.RowIndex].Cells["chk"];
if (!(bool)chktmp.FormattedValue)
{
chktmp.Value = true;
}
for (int i = 0; i < dgvAccount.Rows.Count; i++)
{
if ( i != e.RowIndex)
{
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dgv.Rows[i].Cells["chk"];
chk.Value = false;
}
}
DataGridViewCheckBoxCell chktmp = (DataGridViewCheckBoxCell)dgv.Rows[e.RowIndex].Cells["chk"];
if (!(bool)chktmp.FormattedValue)
{
chktmp.Value = true;
}
for (int i = 0; i < dgvAccount.Rows.Count; i++)
{
if ( i != e.RowIndex)
{
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dgv.Rows[i].Cells["chk"];
chk.Value = false;
}
}
判断是否选中
for (int i = 0; i < dgv.Rows.Count ; i++)
{
dgv.CommitEdit(DataGridViewDataErrorContexts.Commit);
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dgv.Rows[i].Cells["chk"];
if (chk != null && (bool)chk.FormattedValue)
{
}
}
{
dgv.CommitEdit(DataGridViewDataErrorContexts.Commit);
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dgv.Rows[i].Cells["chk"];
if (chk != null && (bool)chk.FormattedValue)
{
}
}