简体   繁体   中英

How to have only one of two DataGridViewCheckBox columns checked or true in a DataGridView

I have a DataGridView with five columns defined. The user populates the DataGridView at runtime by selecting one or more files through an openFileDialog and the file name(s) are loaded into the first column of the DataGridView. The second and third columns contain checkboxes to indicate how the file name should be processed. The true value is 1 and false is 0. However, the user needs to only be able to select one of the two checkboxes for each file name. If the user checks one of the checkboxes, I want the other checkbox, if checked, to become unchecked. Here is my code below. It doesn't generate any errors and, when it occasionally makes a checkbox become unchecked when another is checked, it may uncheck another checkbox in the same or the other column, but never in the same row. I need to learn to use the debugger and will work on that now while I'm stumped, but would appreciate any help that I can receive in the meantime:

private void dataGridViewInputReports_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex >= 0 && e.ColumnIndex > 0 && e.ColumnIndex < 3)
    {
        if (Convert.ToInt16(dataGridViewInputReports.Rows[e.RowIndex].Cells[e.ColumnIndex].Value) == 1)
        {
            if (e.ColumnIndex == 1)
            {
                dataGridViewInputReports.Rows[e.RowIndex].Cells[2].Value = 0;
            }
            else if (e.ColumnIndex == 2)
            {
                dataGridViewInputReports.Rows[e.RowIndex].Cells[1].Value = 0;
            }
        }
    }
}

The solution is to not use the CellValueChanged event, but the CellClick event. I found this guidance here: https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcheckboxcell(v=vs.110).aspx where it states "If you want to respond immediately when users click a check box cell, you can handle the DataGridView.CellClick event, but this event occurs before the cell value is updated. If you need the new value at the time of the click, one option is to calculate what the expected value will be based on the current value." So, if the user clicks on a checkbox that is checked (= 1), I have to write my code knowing that the value of the checkbox will become unchecked (= 0), but for now it's still checked (= 1). This code will toggle which of two checkboxes are checked in the same dataGridView row:

private void dataGridViewInputFiles_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex >= 0 && e.ColumnIndex == 1 || e.ColumnIndex == 2)
    {
        if (Convert.ToInt16(dataGridViewInputFiles.Rows[e.RowIndex].Cells[e.ColumnIndex].Value) == 0)
        {
            if (e.ColumnIndex == 1)
            {
                dataGridViewInputFiles.Rows[e.RowIndex].Cells[2].Value = 0;
            }
            else if (e.ColumnIndex == 2)
            {
                dataGridViewInputFiles.Rows[e.RowIndex].Cells[1].Value = 0;
            }
        }
        else
        {
            if (e.ColumnIndex == 1)
            {
                dataGridViewInputFiles.Rows[e.RowIndex].Cells[2].Value = 1;
            }
            else if (e.ColumnIndex == 2)
            {
                dataGridViewInputFiles.Rows[e.RowIndex].Cells[1].Value = 1;
            }
        }
    }
}

You can refer to this link to get an idea of how to do operation on multiple selects. This is for multiple select for delete. This might be helpful for your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM