Windows Develop Bookmark and Share   
 index > Windows Forms General > How to make checkboxes in DataGridView mutually exclusive?
 

How to make checkboxes in DataGridView mutually exclusive?

Hi,

I have a DataGridView which has a DataGridViewCheckBoxColumn. It data binds fine. However, I need to make all the checkboxes in the grid mutually exclusive, so that if one is checked, all the others are unchecked.  This should happen as soon as the user checks a column.

Any suggestions?

Thanks.

Dan
Dan Hurwitz
dhurwitz  9 hours 47 minutes ago
Hi,

In Oncellcontentclick event you can use the code below.
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn)
            {
                bool state = Convert.ToBoolean(((DataGridViewCheckBoxCell)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]).EditingCellFormattedValue);
                foreach (DataGridViewRow dgvr in dataGridView1.Rows)
                {
                    ((DataGridViewCheckBoxCell)dgvr.Cells[e.ColumnIndex]).Value = state;
                }
            }
        }
Tamer Oz  9 hours 31 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• how to upgrade toolbox to .net 3.5 versions of all controls?
• Listbox with icons
• Changing Index to Font and Size comboboxes, in a RixhTextBox.
• Convert MFC Fonts to c# Fonts
• Threading UI
• RichTextBox - finding word at a position
• CodeDom & InitializeComponent
• Strange backgroundworker and datagridview problem.
• Combobox Drop Down
• DataGridView in C#