Hello people,
I've done a search but nothing specific has come up so any help would be hugely appreciated.
Basically I have a table with a column in it called MedCheckStat which has either 0, 1 or 2 in it indicating the state. I'm in the process of converting an Access application into a .NET application - for this column the Access application uses a group of check boxes that effectively work as radio buttons that flip between the values. In .NET however I am using DataGridViews and so means I have to find a new way of doing it.
If I had my way I would just extract the states into a new database and link the english labels via foreign keys but I've been told not to do this. Instead I thought I'd just create a DataTable in my code and use that to drive a combobox in the datagridview as all the other comboboxcolumns had worked perfectly (the ones using strongly typed DataTables generated from the DataSet Designer in VS2008.
But at runtime I get this error:
System.FormatExceptiuon: DataGridViewComboBoxCell value is not valid
and the relevant code:
DataGridViewComboBoxColumn consentCol = new DataGridViewComboBoxColumn();
// Create a DataTable.
DataTable dt = new DataTable("Temporary");
// Create a DataColumn and set various properties.
DataColumn column1 = new DataColumn();
column1.DataType = System.Type.GetType("System.Int32");
column1.ColumnName = "Value";
DataColumn column2 = new DataColumn();
column2.DataType = String.Empty.GetType();
column2.ColumnName = "Display";
// Add the column to the table.
dt.Columns.Add(column1);
dt.Columns.Add(column2);
// Add 10 rows and set values.
DataRow row;
row = dt.NewRow();
row["Value"] = 0;
row["Display"] = "No Information Given";
dt.Rows.Add(row);
row = dt.NewRow();
row["Value"] = 1;
row["Display"] = "Consent Given";
dt.Rows.Add(row);
row = dt.NewRow();
row["Value"] = 2;
row["Display"] = "Consent Withheld";
dt.Rows.Add(row);
consentCol.HeaderText = "Consent";
consentCol.DataPropertyName = "MedCheckStat";
consentCol.DataSource = dt;
consentCol.ValueMember = "Value";
consentCol.DisplayMember = "Display";
consentCol.AutoComplete = true;
consentCol.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
consentCol.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
consentDataGridView.Columns.Add(consentCol);
It seems the error generally happens when two values dont match, but the column only has 0, 1 and 2 in it and I've created rows for each of those - so I'm pretty stumped, any help as I said is appreciated.
Marlon