I need some help to understand why my application is raising DataError errors for a DataGridViewComboBoxColumn of a DataGridView. The error does not always occur, it seems to only happen in a limited scenario.
I have a DataGridView (dgvFuel) that has a DataGridViewComboBoxColumn. This grid shows the details for records in the Fuel table, including the name of the provider for each fuel purchase (e.g., "Shell Oil" or "Costco"). The Fuel table includes the ID of the provider, but not the provider's name, so I use the DisplayMember of the DataGridViewComboBoxColumn to show the provider's name in the grid.
There is a parent-child relationship between the Provider table and Fuel table (a single provider can have multiple fuel records). The Fuel table has a foreign key field (ProviderID) that is linked to the primary key field of the Providers table. Here is the Relation...
The dgvFuel.DataGridViewComboBoxColumn is configured as follows...
The problem that I am having occurs when I add a new Provider record and then immediately use the newly added Provider when adding a new Fuel record. After I've added the new Fuel record, the dgvFuel.DataError is raised (multiple times). The DataGridViewDataErrorEventArgs Context and Exception.Message properties are:
Context = "Formatting, PreferredSize", Exception.Message = "DataGridViewComboBoxCell value is not valid."
Context = "Formatting, Display", Exception.Message = "DataGridViewComboBoxCell value is not valid."
The dgvFuel.DataGridViewComboBoxColumn does not show the Provider for the new Fuel record.
I have confirmed that the Providers DataTable has the newly added DataRow (and also that the newly added DataRow has been saved to the underlying data source).
If I close and restart the application, then the dgvFuel.DataGridViewComboBoxColumn shows the Provider for the new Fuel record *and* when I add a new Fuel record for that Provider the dgvFuel.DataGridViewComboBoxColumn shows the Provider for the new Fuel record.
The problem seems to only occur for the scenario where I add a new Provider and then a new Fuel record for that Provider.
What is going on and how can I fix this problem?
I have a DataGridView (dgvFuel) that has a DataGridViewComboBoxColumn. This grid shows the details for records in the Fuel table, including the name of the provider for each fuel purchase (e.g., "Shell Oil" or "Costco"). The Fuel table includes the ID of the provider, but not the provider's name, so I use the DisplayMember of the DataGridViewComboBoxColumn to show the provider's name in the grid.
There is a parent-child relationship between the Provider table and Fuel table (a single provider can have multiple fuel records). The Fuel table has a foreign key field (ProviderID) that is linked to the primary key field of the Providers table. Here is the Relation...
Code:
dsMain.Relations.Add("Provider_Fuel", dsMain.Tables("Providers").Columns("RecID"), dsMain.Tables("Fuel").Columns("ProviderID"))
fkc = dsMain.Relations("Provider_Fuel").ChildKeyConstraint
fkc.DeleteRule = Rule.Cascade
fkc.UpdateRule = Rule.Cascade
fkc.AcceptRejectRule = AcceptRejectRule.None
Code:
cbc = New DataGridViewComboBoxColumn
With cbc
.DataPropertyName = "ProviderID"
.DataSource = dsMain.Tables("Providers")
.ValueMember = "RecID" '...field from the Providers data table
.DisplayMember = "Provider" '...binds the ComboBox to the "Provider" column of the DataViewGrid's DataSource
.Name = "Provider"
.HeaderText = "Provider"
.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing '...when it is not in edit mode, the DataGridViewComboBoxCell is displayed without a drop-down button
.FlatStyle = FlatStyle.Flat '...gets rid of the DataGridViewComboBoxCell highlighting and border effects
.SortMode = DataGridViewColumnSortMode.Automatic '...sorting glyph is automatically displayed in the column header
End With
.Columns.Add(cbc)
Context = "Formatting, PreferredSize", Exception.Message = "DataGridViewComboBoxCell value is not valid."
Context = "Formatting, Display", Exception.Message = "DataGridViewComboBoxCell value is not valid."
The dgvFuel.DataGridViewComboBoxColumn does not show the Provider for the new Fuel record.
I have confirmed that the Providers DataTable has the newly added DataRow (and also that the newly added DataRow has been saved to the underlying data source).
If I close and restart the application, then the dgvFuel.DataGridViewComboBoxColumn shows the Provider for the new Fuel record *and* when I add a new Fuel record for that Provider the dgvFuel.DataGridViewComboBoxColumn shows the Provider for the new Fuel record.
The problem seems to only occur for the scenario where I add a new Provider and then a new Fuel record for that Provider.
What is going on and how can I fix this problem?