Adding a row by clicking on the Add button in a DataGridView

204 Views Asked by At

I am working on a Windows Forms application and I have a grid view which lists all the users found in a database. Database looks like this:

table Users

table Privileges

Now, every user can have only one privilege, like Admin, or User etc, but I keep this info in a separate table and have a foreign key constraint defined in such way that an end user can't insert a user with a wrong privilege but rather, just from those which are found in Privileges table.

By the way, I am using Entity Framework and I have created my Model object using Database First method. But this likely doesn't affect on a question/answer because logic of what I am going to ask is probably the same, no matter how do you manipulate with data from database.

So, right now, I have two binding sources, one is called userBidningSource, and other is privilegesBindingSource. User model has a navigation property to Privileges and vice-versa (this is generated by EF). Now I have dragged a user data source from Data Sources menu, to my form, and wrote some code (in my form load) like this:

try{
   context.user.Load();
   this.userBindingSource.DataSource = context.user.Local.ToBindingList();

}catch(Exception ex){
    Debug.Write(ex);
}

Now this works (I probably forgot to add all code, but until now everyhing works) and I get a list of all users with all the columns from a Users table.

Now because it is unsafe and doesn't make sense to leave to the end user to choose/guess what privilege_id field should be entered, I made a combobox on a grid view for that column.

So I added something like this in my code:

 context.privileges.Load();
       this.privilegesBindingSource.DataSource = context.user.Local.ToBindingList();

Also I have set data source of this column in a designer (right click on a grid view , then Edit Columns) to the privileges datasource.

Now all drop downs are filled as it should.

The Problem

Finally, the problem... Right now, I am trying to add a new row. So if I click on + icon of a grid view, I get an error which says :

"DataGridViewComboBoxCell value is not valid. To replace this default dialog please handle the DataError event."

So the question is, how to add a row with a current setup ? What I am doing wrong?

2

There are 2 best solutions below

1
On BEST ANSWER

Try adding the event DataError by going to the dataGridView properties and leave the function body empty/blank.

6
On

You need to set the DisplayMember and the ValueMember properties so when a new row is added, it will use that knowledge to populate the combobox. You may also want to add a method so when a new row is requested, it will call your handler so you can specify what the default values for the new row should be. You can add an event handler to the DefaultValuesNeeded event and specify defaults like this for example:

private void dataGridView1_DefaultValuesNeeded(object sender,
    System.Windows.Forms.DataGridViewRowEventArgs e)
{
    e.Row.Cells["Region"].Value = "WA";
    e.Row.Cells["City"].Value = "Redmond";
    e.Row.Cells["PostalCode"].Value = "98052-6399";
    e.Row.Cells["Country"].Value = "USA";
    e.Row.Cells["CustomerID"].Value = NewCustomerId();
}