简体   繁体   中英

Using custom CheckBoxComboBox in DataGridView column

I need to use the combobox control with items that can be checked in the DataGridView column. So i have found one control from the following link https://github.com/sgissinger/CheckBoxComboBox/

In this the basic CheckBoxComboBox is working fine in the demo and it has extended DataGridViewCheckBoxComboBoxColumn class that can be used in DataGridView for a single column.

Now on adding this column in DataGridView on design time, with DataGrid enabled to add new rows, when a data is inserted in another column and value is selected from this column, the ParseFormattedValue() is giving null exception.

Also when adding a new row using code, then GetFormattedValue() is giving null exception in value parameter of it.

In DataGridView I have added two columns, one is simple textbox column and another is DataGridViewCheckBoxComboBoxColumn adding week day names in it.

Now when I try to add a new row by

dataGridView1.Rows.Add();

its showing exception in GetFormattedValue() as null is coming in value parameter of it.

The use of custom CheckBoxComboBox control in DataGridView is done. First I have added a DataGridViewTextBoxColumn or any other as per your requirement and then add the custom CheckBoxComboBox column in that DataGridView in following way. First you need to create the list of items to be shown in that Combo Box

 List<Status> statuses = new List<Status>();
 statuses.Add(new Status(1, "Sunday"));
 statuses.Add(new Status(2, "Monday"));
 statuses.Add(new Status(3, "Tuesday"));
 statuses.Add(new Status(4, "Wednesday"));
 statuses.Add(new Status(5, "Thursday"));
 statuses.Add(new Status(6, "Friday"));
 statuses.Add(new Status(7, "Saturday"));

Then you need to create the object of DataGridViewCheckBoxComboBoxColumn

DataGridViewCheckBoxComboBoxColumn comboboxColumn = new DataGridViewCheckBoxComboBoxColumn();

Create an object of ListSelectionWrapper from that statuses List object and set its TextSeparator property.

ListSelectionWrapper<Object> wrappedList = new ListSelectionWrapper<Object>(statuses);
wrappedList.TextSeparator = comboboxColumn.TextSeparator;

Add comboboxColumn other properties as

 comboboxColumn.DataSource = wrappedList;
 comboboxColumn.ValueMember = "Selected";
 comboboxColumn.DisplayMemberSingleItem = "Name";
 comboboxColumn.DisplayMember = "NameConcatenated";

And then insert the column in DataGridView

 dgvKioskList.Columns.Add(comboboxColumn);

After this your column is inserted in your DataGridView. Now to insert a new row with pre-selected some list items, you need to create a Dictionary object like

Dictionary<String, Object> objSelectedDays = new Dictionary<String, Object>();

If you want to select the Sunday and Tuesday from the ComboBox for example you could write

objSelectedDays.Add("Sunday", statuses[0]);
objSelectedDays.Add("Tuesday", statuses[2]);

After this when you insert a new row into DataGridView with your other data and this objSelectedDays object, the new row will be added to grid

datagridView1.Rows.Add("ID1", objSelectedDays);

where "ID1" is inserted into simple DataGridViewTextBoxColumn

If you want to read the selected values from the datagrid then type cast the cell into Dictionary object like

var values = datagridView1.Rows[0].Cells[1].Value as Dictionary<String, Object>;

and then loop though the values object array to read the selected values from the particular rows CheckBoxComboBox control

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