Question Datagridview giving errors on adding a new row on the comboboxcell

JanAntwerpen

Member
Joined
Nov 5, 2013
Messages
8
Programming Experience
Beginner
Hello,

I have problems with a comboboxcolumn in a datagridview: As I want to insert a new row, I get a "System.ArgumentException: The value of the DataGridViewComboBoxCell is invalid." I am using Linq to SQL to populate the data from different tables:

- Table Customer (ID (key), Name,...)
- Table Address (ID (key), CustomerID, Street,...)
- Table ContactPerson (ID (key), AddressID, Name, Title,...)

My Linq query to populate the ContactDataGridView(which is bound to the TextBox RelationIdTextBox), is following:
C#:
var y = from r in BH.Relations
                        where r.RelationID.ToString() == relationIDTextBox.Text
                        join v in BH.Addresss on r.RelationID equals v.RelationID
                        join c in BH.ContactPersons on v.AddressID equals c.AddressID
                        select c;
    contactPersonsBindingSource.DataSource = y;
    ContactDataGridView.DataSource = contactPersonsBindingSource;
This works fine.

In this ContactDataGridView I have a ComboBoxCell AddressID which populates the streetnames instead of the AddressID, using following linq query:
C#:
    var addr = from a in BH.Addresss
                           where a.RelationID.ToString() == relationIDTextBox.Text
                           select new { AddressID = a.AddressID, Street = a.Street };
    ContactAddressBindingSource = addr;
This works fine as well.

That is, untill I want to add a new row in this datagridview. Then I get the error "System.ArgumentException: The value of the DataGridViewComboBoxCell is invalid."

Can anyone help to make me understand what I am doing wrong? I really cannot figure it out!

Thanks!
 
That means that the value in that cell for the AddressID does not match any AddressID in the list bound to the combo box column. I'm guessing that the AddressID defaults to zero and there is no zero value in the drop-down list. If so then that's your issue. Every value in a cell in that column must either match a value in the drop-down list or be DBNull.Value. If your AddressID column is not nullable then then it can't be DBNull.Value so you would have to set it to a valid default value. Better to make it nullable in your entity class, even if it's not in the database.
 
I tried it with other tables within my Database, and I keep getting the same errors. I noticed that I only get this error when the comboboxcell may not contain null values. Now, how can I prevent this error and let the user decide which value he wants to select from within the possible values in this combobox?
 

Latest posts

Back
Top Bottom