DataGridView - how can I make a checkbox as a switch?

I have a Windows Forms application that displays a list of objects in a DataGridView .

This control displays bool values โ€‹โ€‹as checkboxes.

In the properties of an object that are mutually exclusive, there are three flags. No more than one of them can be true. Accordingly, I would like the checkboxes to act as a set of radio buttons.

Just a side note from the old guy: I think that these days people donโ€™t even know why they are called radio buttons. Previously, there were 4 or 5 buttons on the radio in the car, and the depressing of any of them made everyone else pop up. They were mutually exclusive. The radio button is probably not a useful description these days because the radio no longer has such buttons, I donโ€™t think.

How can i do this? I believe that if I attach the "CheckedChanged" event to the checkboxes, and I know the line, I can find all the other checkboxes.

What event can I capture to capture the checkbox control when it is first rendered so that I can attach the CheckedChanged event to it? I know about DataGridView.CellFormatting, but I think this is wrong because it is called every time it draws a DataGridView. I really need an event that is fired only the first time the DGV is displayed.

+3
source share
4 answers

The one you want is CellContentClick, on the DGV itself. Attach a handler that checks if this column is a DGV CheckBoxCell, and if so, check all the other checkboxes in the row.

, CheckBoxCell , . , , , , . , , , , , ( , ). , CellValueChanged , , , , .

+1

KeithS .

CellValueChanged, :

DataGridView.CellValueChanged , , , .

, , . , DataGridView.CurrentCellDirtyStateChanged. , , DataGridView.CommitEdit Commit.

, :

    void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
    {
        // Manually raise the CellValueChanged event
        // by calling the CommitEdit method.
        if (dataGridView1.IsCurrentCellDirty)
        {
            dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
        }
    }

    public void dataGridView1_CellValueChanged(object sender,
                                               DataGridViewCellEventArgs e)
    {
        // If a check box cell is clicked, this event handler sets the value
        // of a few other checkboxes in the same row as the clicked cell.
        if (e.RowIndex < 0) return; // row is sometimes negative?
        int ix = e.ColumnIndex;
        if (ix>=1 && ix<=3)
        {
            var row = dataGridView1.Rows[e.RowIndex];

            DataGridViewCheckBoxCell checkCell =
                (DataGridViewCheckBoxCell) row.Cells[ix];

            bool isChecked = (Boolean)checkCell.Value;
            if (isChecked)
            {
                // Only turn off other checkboxes if this one is ON. 
                // It ok for all of them to be OFF simultaneously.
                for (int i=1; i <= 3; i++)
                {
                    if (i != ix)
                    {
                        ((DataGridViewCheckBoxCell) row.Cells[i]).Value = false;
                    }
                }
            }
            dataGridView1.Invalidate();
        }
    }
+7
  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);
        }
    }
+1
source

It will be too easy here:

Consolidate checkbox column is the second column in your datagridview document.

private void YourDatagridview_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
         if (IsHandleCreated)
        {
            if (YourDatagridview.CurrentCell == YourDatagridview.Rows[e.RowIndex].Cells[1])
            {
                if (Convert.ToBoolean(YourDatagridview.CurrentCell.Value) == true)
                {
                    for (int i = 0; i < YourDatagridview.RowCount; i++)
                    {
                        if (YourDatagridview.Rows[i].Cells[1] != YourDatagridview.CurrentCell)
                        {
                            YourDatagridview.Rows[i].Cells[1].Value = false;
                        }
                    }
                }
            }
        }
    }

And call it too:

private void YourDatagridview_CurrentCellDirtyStateChanged(object sender, EventArgs e)
    {
        if (this.YourDatagridview.IsCurrentCellDirty)
        {
            YourDatagridview.CommitEdit(DataGridViewDataErrorContexts.Commit);
        }
    }

and Voila !!

0
source

All Articles