I've found several topics with a similar issue but the contents seemed slightly different and I have not been able to solve my problem as a result.
In my application I have DataTable that contains 4 columns:
- UInt16
- UInt64
- UInt64
- A self defined enumeration type consisting of 2 values.
In order to display the values of this DataTable I created a DataGridView and set this table as the datasource. So far so good. I wanted the enumeration field column to contain comboboxes instead and ran into the DataGridViewComboBoxColumn type. I had some issues getting it to work but eventualy ended up using the following approach:
// Create the 4 datarows
// Add the datarows to the data table
// Set the data table as the data source for the data grid view
// Remove the column that represents the enumeration
// Add a DataGridViewComboBoxColumn to the DataGridView as replacement
I have created the DataGridViewComboBoxColumn as follows:
DataGridViewComboBoxColumn cb = new DataGridViewComboBoxColumn();
cb.HeaderText = "My header text";
cb.ValueType = typeof(MyEnumType);
cb.DataSource = Enum.GetValues(typeof(MyEnumType));
cb.FlatStyle = FlatStyle.System;
cb.Name = "Name"; //Same name as the DataColumn and the now deleted DataGridViewColumn
cb.DataPropertyName = "Name"; //Same name as the DataColumn and the now deleted DataGridViewColumn
cb.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
dataGridView.Columns.Add(cb);
Once my application starts I read in data from a text file that get placed into a structure with fields of the 4 datatypes mentioned above. I then add these fields to the DataTable as follows:
DataRow row = dataTable.NewRow();
row["Name of the UInt16 column"] = mystruct.theUInt16;
row["Name of the UInt64 column"] = mystruct.theUInt64;
row["Name of the UInt64 column"] = mystruct.theUInt64_2;
row["Name of the enum column"] = mystruct.theEnumValue;
dataTable.Rows.Add(row);
on startup the DataError event gets called repeatedly. The contents of the cells do get filled properly however. (I see this after clicking away the error a couple of times) Disabling the DataError event (assigning an empty handler for example) is something I prefer to not do.
I think there is some sort of type mismatch somehow. (maybe the enum type and a string for display?) This is however only a guess. The dataTable column and the datagridview column both have the type set to the enumeration.
I hope someone can point me in the right direction.
Thanks in advance!