Datagridview forcing only one checkbox to be selected in a column

后端 未结 9 1312
野性不改
野性不改 2020-12-21 07:29

How do I force only a single checkbox to be checked in a column of a Datagridview?

9条回答
  •  生来不讨喜
    2020-12-21 08:04

        private void dataGridViewProduit_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if ((sender as DataGridView).CurrentCell is DataGridViewCheckBoxCell)
            {
                if (Convert.ToBoolean(((sender as DataGridView).CurrentCell as DataGridViewCheckBoxCell).Value))
                {
                    foreach (DataGridViewRow row in (sender as DataGridView).Rows)
                    {
                        if (row.Index != (sender as DataGridView).CurrentCell.RowIndex && Convert.ToBoolean(row.Cells[e.ColumnIndex].Value) == true)
                        {
                            row.Cells[e.ColumnIndex].Value = false;
                        }
                    }
                }
            }
        }
    
        private void dataGridViewClient_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (this.dataGridViewClient.IsCurrentCellDirty)
            {
                dataGridViewClient.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }
    

提交回复
热议问题