Search code examples
c#winformsdatagridviewdatagridviewcomboboxdatagridviewcomboboxcell

List of CheckBox in DataGridViewComboBoxColumn [Windows Form]


I am trying to show a dropdown composed of several CheckBoxes (a list of CheckBox) in a DataGridView. Specifically, the DataGridView provide a DataGridViewComboBoxColumn that allows to have a dropdown with several items. However, if you try to add as a datasource of the column a list of checkboxes you will find out that the solution is not working (the checkboxes are not shown).

In the following link there is a solution on "regular" ComboBox: https://www.codeproject.com/Articles/31105/A-ComboBox-with-a-CheckedListBox-as-a-Dropdown

However, I need it for DataGridViewComboBoxColumn. Someone has an idea of what can be done to reach the goal? Thanks for answering (I link an example of the code below)

for (int i = 0; i < dataGridView.Rows.Count; i++)
{
    // Put List of Checkboxes in DataGridViewComboBoxCell for each row of the Grid
    ((DataGridViewComboBoxCell) dataGridView.Rows[i].Cells[1]).DataSource = new List<CheckBox> { new CheckBox(), new CheckBox(), new CheckBox(), new CheckBox() };
}

Solution

  • I have modified DataGridViewColumns in past (OK, not entirely myself, according to some CodeProject cook book too :-) ), so I have some idea.

    However this can't probably work in this case, there's a problem with this idea. You'd not only need to change how the cell looks, but also think how to store the data. How would you assign a separate datasource to each single DataGridViewComboboxCell? If possible, that would be probably quite extensive work, probably a class handling the structure and the events.

    If I can offer a solution, you can have another object (i.e. another DataGridViw at the side with CheckBoxColumn and TextColumn which would load on DataGridView1.SelectionChanged), or you can populate it in a modal form on CellClick of that cell and feed back a list of selected items strings on close, etc. I did that in past many times and I don't think there's an easy way around it.