In datagridview how to use checkbox as radiobutton?

12,737

Solution 1

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        //clean al rows
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            row.Cells["Select"].Value = false;
        }

        //check select row
        dataGridView1.CurrentRow.Cells["Select"].Value = true;
     }

Solution 2

I know I'm late to the party, but here is code to use actual radio buttons instead of checkboxes.

Credit:
Thanks to Arsalan Tamiz's blog post, on which this code is based.
http://arsalantamiz.blogspot.com/2008/09/using-datagridview-checkbox-column-as.html
Also thanks to M. Viper's answer for laying some of the ground work.

Step 1: Add a DataGridView control to your panel and add a CheckBoxColumn (I named mine grid and colRadioButton respectively). I'm not sure if this matters, but the EditMode property of my DataGridView is set to EditOnEnter.

Step 2: Create an event handler for the CellPainting event.

private void grid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.ColumnIndex == colRadioButton.Index && e.RowIndex >= 0)
    {
        e.PaintBackground(e.ClipBounds, true);

        // TODO: The radio button flickers on mouse over.
        // I tried setting DoubleBuffered on the parent panel, but the flickering persists.
        // If someone figures out how to resolve this, please leave a comment.

        Rectangle rectRadioButton = new Rectangle();
        // TODO: Would be nice to not use magic numbers here.
        rectRadioButton.Width = 14;
        rectRadioButton.Height = 14;
        rectRadioButton.X = e.CellBounds.X + (e.CellBounds.Width - rectRadioButton.Width) / 2;
        rectRadioButton.Y = e.CellBounds.Y + (e.CellBounds.Height - rectRadioButton.Height) / 2;

        ButtonState buttonState;
        if (e.Value == DBNull.Value || (bool)(e.Value) == false)
        {
            buttonState = ButtonState.Normal;
        }
        else
        {
            buttonState = ButtonState.Checked;
        }
        ControlPaint.DrawRadioButton(e.Graphics, rectRadioButton, buttonState);

        e.Paint(e.ClipBounds, DataGridViewPaintParts.Focus);

        e.Handled = true;
    }
}

Step 3: Handle the CurrentCellDirtyStateChanged event to uncheck the previous selection. This is basically the same as M. Viper's answer.

private void radioButtonChanged()
{
    if (grid.CurrentCell.ColumnIndex == colRadioButton.Index)
    {
        foreach (DataGridViewRow row in grid.Rows)
        {
            // Make sure not to uncheck the radio button the user just clicked.
            if (row.Index != grid.CurrentCell.RowIndex)
            {
                row.Cells[colRadioButton.Index].Value = false;
            }
        }
    }
}

private void grid_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
    radioButtonChanged();
}

Step 4: (Optional) Handle the CellClick event to allow the user to check the radio button by clicking anywhere in the cell rather than only directly on the radio button.

private void grid_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == colRadioButton.Index)
    {
        DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)grid.Rows[e.RowIndex].Cells[colRadioButton.Index];
        cell.Value = true;
        radioButtonChanged();
    }
}
Share:
12,737
user3410150
Author by

user3410150

Updated on June 04, 2022

Comments

  • user3410150
    user3410150 almost 2 years

    IDE: Visual Studio c#, Winforms Application.

    I have invested around 12 hours but didn't get success. As DataGridView don't provide radiobutton type of cell. so i am trying to use checkbox cell as radio-buttion functionality.

    i.e I want to be checked only one checkbox in a column.

    see image:

    enter image description here

    It looks very simple thing but trust me it is not as simple as we are thinking. before giving reply please test the code.

    Here are my sample tested code which i have tried:

    code 1

    ////start
    if (e.RowIndex != -1)
    {
        if (dataGridView1.Rows[e.RowIndex].Cells[0].Value != null && dataGridView1.CurrentCell.ColumnIndex == 0) //null check
        {
            if (e.ColumnIndex == 0)
            {
                if (((bool)dataGridView1.Rows[e.RowIndex].Cells[0].Value == true))
                {
    
                    for (int k = 0; k <= 4; k++)
                    {
                        //uncheck all other checkboxes but keep the current as checked
                       if (k == dataGridView1.CurrentRow.Index)
                        {
                            dataGridView1.Rows[k].Cells[0].Value = false;
                     }
                        //if (gvTeam1.Rows[k].Cells[2].Selected != null)
                        //if(k !=e.RowIndex)              
    
                    }
    
                    // gvTeam1.Rows[e.RowIndex].Cells[2].Value = false; // keep the current captain box checked
                }
            }
            //}
    
    
            // gvTeam1.Rows[rowPointerTeam1].Cells[2].Value = true;
        }
    }
    //end
    // here gvTeam1 is Datagridview1
    

    code 2: tested on datagridview1

    private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
    {
        if (dataGridView1.CurrentCell.ColumnIndex == 0)
        {
            for (int i = 0; i < 8; i++)
            {
                //if (i != dataGridView1.CurrentCell.RowIndex)
                    dataGridView1.Rows[i].Cells[0].Value = false;              
    
            }
            dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[0].Value = true;
        }
    }
    
  • user3410150
    user3410150 about 10 years
    Not working man. THis code is unchecking all and current checkbox when i am leaving from that cell.
  • user3410150
    user3410150 about 10 years
    Now on 5th click i am able to check the checkbox and when moving to next column Its again unChecking the previous checkbox.
  • user3410150
    user3410150 about 10 years
    can you provide me dll. or sample solution, of that grid which u have built.
  • Junaith
    Junaith about 10 years
    Which one you are asking? The DataGridView with CheckBoxColumn behaving like radio button? I don't have it as we replaced it with the custom RadioButtonCell from the MSDN link.
  • user3410150
    user3410150 about 10 years
    okay then send me your custom RadioButtonCell content. , solution. you can share the link on [email protected], you can share me the dropbox link.
  • Arvind
    Arvind over 4 years
    This allows the user to check same checkbox twice and end up with an unchecked current row (which means no row is checked). This is not the radio button behavior desired by original poster. How to force the current checkbox to stay checked no matter how many times it is clicked?