DataGridViewComboBoxes for numeric fields

  • Thread starter Thread starter Philip Sheard
  • Start date Start date
P

Philip Sheard

I have several integer fields which have small fixed ramges of values. I
have tried to display them as DataGridViewComboBoxes, but it fails, because
the fields are numeric.

Is there an easy way round this? At the moment I am displaying them as text
boxes, which is not ideal.
 
It doesn't matter if the fields are numeric. Otherwise, it wouldn't work in
a *text* box either.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Complex things are made up of
lots of simple things.
 
More information. I get "DataGridViewComboBoxCell value is not valid." when
the form is displayed. Values can be changed, but not saved.
 
Need more information. Details and specifics. All you've done so far is give
us the 10,000-foot view of your requirements and end-user experience.
Diagnostics require data.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Complex things are made up of
lots of simple things.
 
Very well.

My observation is that DataGridViewComboBoxes *never* work for numeric
fields. Your argument, apparently based on some kind of Aristotelian view of
nature, is that they must work, because DataGridViewTextBoxes can handle
numeric fields.

As an experiment, create:
1. an SQL Server table, with just 2 columns - an identity field and an int.
2. a VS 2005 project.
3. a data data source, using the new table.
4. a Windows form.
5. a DataGridView, by dragging the data source onto the form.

Change the column type of the int field to DataGridViewComboBox, and add
some values to the dropdown list. When you run the project, you will get the
message "DataGridViewComboBoxCell value is not valid.", repeatedly.
 
Philip said:
Very well.

My observation is that DataGridViewComboBoxes *never* work for numeric
fields. Your argument, apparently based on some kind of Aristotelian view
of nature, is that they must work, because DataGridViewTextBoxes can
handle numeric fields.

As an experiment, create:
1. an SQL Server table, with just 2 columns - an identity field and an
int. 2. a VS 2005 project.
3. a data data source, using the new table.
4. a Windows form.
5. a DataGridView, by dragging the data source onto the form.

Change the column type of the int field to DataGridViewComboBox, and add
some values to the dropdown list. When you run the project, you will get
the message "DataGridViewComboBoxCell value is not valid.", repeatedly.
I don't know whether I am getting this right. Each row has a single value
for the int column; there is not a multiple of values are needed by the
coomboboxes. So it seams to me that you would need to provide multiple
values in the row for the combobox as a detail of the main datagrid.
 
OK, Philip, just to be sure I wasn't being "Aristotelian," I created a new
project, added a DataSource using the SQL Server Northwind database, two
tables, Employees (ID (int(, LastName, FirstName), and Orders (EmployeeID
and OrderDate). I dragged the Orders table to the form, and edited the
EmployeeID column to use a DataGridViewComboBoxColumn, with Employees as the
DataSource, Employees.EmployeeID as the DataMember, and
Employees.EmployeeeID as the Value Member. I then ran the form, with *no
problems*. The Employee Number showed up in the ComboBox column, and I was
able to change it to the Employee ID of any Employee in the database.

I've been putting in 70-hour weeks for several months now, so I hope you
appreciate it! ;-)

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

The man who questions opinions is wise.
The man who quarrels with facts is a fool.
 
Yes, of course it works when the values are in another table, but not when
they are stored in the combo box itself.
 
My observation is that DataGridViewComboBoxes *never* work for numeric
Yes, of course it works when the values are in another table, but not when
they are stored in the combo box itself.

The field I used from the Employees table *is* a numeric field. And the
values in it are stored in the Items Collection of the combo box itself.
There is no other place to store them.

The problem you are having therefore has nothing to do with numeric fields.
It has to do with how you're putting values into the
DataGridViewComboBoxColumn, or what those values are. For example, if the
column is bound to a table data source, the values must be in the table it
is bound to or they will not be valid.

There are 4 distinct properties of a DataGridViewComboBoxColumn that come
into play when binding it to a table:

DataSource: The table (object) that the Items Collection is populated from.
If this property is set, you can not use the Items Collection to populate
the DataGridViewComboBoxColumn.

ValueMember: The column in the table that holds the underlying value for
each Item (row).

DisplayMember: The column in the table that holds the displayed value for
each Item (row).

DataPropertyName: The column in the table (BindingSource) that the
DataGridView is bound to. When the value in the DataGridViewComboBoxCell
changes, the value in the BindingSource is changed.

The Items property can be used when the DataSource is not set.

It is important to remember that a DataGridViewComboBoxColumn can be bound
to 0, 1, or 2 different tables. In your case, it sounds like you only want
to bind to a single table. Therefore, the DataSource property should not be
set.

Finally, make sure that your Data Type (the type that you are assigning to
the Items in the Items Collection) is correct if you populate the Items
Collection by hand. The underlying value must be of the same Data Type as
the Data Type of the table column that the DataPropertyName is referring to.
The value displayed will almost always be a string (although it may be an
image). In any case, it must be something human-readable.

For more information, see:
http://msdn2.microsoft.com/en-US/library/system.windows.forms.datagridviewcomboboxcolumn.aspx

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

The man who questions opinions is wise.
The man who quarrels with facts is a fool.
 
I was having the same issue, it was being caused by Null values. It
would be difficult to define a foriegn key reference in a database table
where the value would never be null! So, this is a bug, however the
proper way to handle it is to simply handle DataError event and tell the
base class to ignor the exception. Obviously you could be more specific
in the event and drill down to the correct column.

void mfg_edit_viewDataGridView_DataError(object sender,
DataGridViewDataErrorEventArgs e)
{
e.ThrowException = false;
}
 
Back
Top