Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > System FormatException - DataGridViewComboBoxCell value is not valid
 

System FormatException - DataGridViewComboBoxCell value is not valid

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


  • Edited bymarlon_tucker Wednesday, September 09, 2009 4:37 PMspelling
  •  
marlon_tucker  Wednesday, September 09, 2009 4:36 PM
found the problem, state was stored as a tiny int in the database, so I needed to use System.Byte as the type
  • Marked As Answer bymarlon_tucker Thursday, September 10, 2009 7:54 AM
  •  
marlon_tucker  Thursday, September 10, 2009 7:54 AM
found the problem, state was stored as a tiny int in the database, so I needed to use System.Byte as the type
  • Marked As Answer bymarlon_tucker Thursday, September 10, 2009 7:54 AM
  •  
marlon_tucker  Thursday, September 10, 2009 7:54 AM

You can use google to search for other answers

Custom Search

More Threads

• Help with adding new record via editbox (datagridview) bounded to a datatable/dataset
• How do you add a hyperlink to a windows forms datagrid?
• Override the return key's default selection in datagridview
• Combobox and Datagridview Data's
• override a treenode class in c#
• Adding a ComboBox to a Single Cell w/i a DataGridView Databined
• need help with table creation
• DataGridComboBoxColumn Sorting on Foreign Key Index Rather than Foreign Value
• How to run time grey out a value (read only) in property grid?
• How to: Concerning Datagrid