Reputation: 185
I keep getting an error that states DataGridViewComboBox value is not valid
. It seems like it is also in an endless loop: I will click ok and it will continuously keep popping up. I am running a program with a windows form application written in C# and .NET. Does anyone know how to fix this error?
Here is some portions of my code:
// authorityTypeDataGridViewTextBoxColumn
//
this.authorityTypeDataGridViewTextBoxColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
this.authorityTypeDataGridViewTextBoxColumn.DataPropertyName = "AuthorityType";
this.authorityTypeDataGridViewTextBoxColumn.DataSource = this.AuthorityTypeBindingSource;
this.authorityTypeDataGridViewTextBoxColumn.DisplayMember = "Description";
this.authorityTypeDataGridViewTextBoxColumn.DisplayStyle = System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox;
this.authorityTypeDataGridViewTextBoxColumn.Frozen = true;
this.authorityTypeDataGridViewTextBoxColumn.HeaderText = "AuthorityType";
this.authorityTypeDataGridViewTextBoxColumn.MaxDropDownItems = 100;
this.authorityTypeDataGridViewTextBoxColumn.Name = "authorityTypeDataGridViewTextBoxColumn";
this.authorityTypeDataGridViewTextBoxColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.authorityTypeDataGridViewTextBoxColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
this.authorityTypeDataGridViewTextBoxColumn.ValueMember = "Value";
this.authorityTypeDataGridViewTextBoxColumn.Width = 121;
//
// AuthorityTypeBindingSource
//
this.AuthorityTypeBindingSource.DataMember = "AuthorityType";
this.AuthorityTypeBindingSource.DataSource = this.lookUpDataSet;
Does anyone have any suggestions?
Here is the Handler:
private void TaskSummaryGrid_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
MessageBox.Show(this, e.Exception.Message);
e.Cancel = true;
}
Upvotes: 3
Views: 8131
Reputation: 2840
Ten years later now, I recently ran into the same problem and ended up working through the datagridview combobox source code to understand the problem and the solution. It turns out that even though swallowing error events is rarely the right course, @mamoun was correct: capturing the error and discarding it may be the correct solution in some scenarios. Here's why.
In a DataGridView with a ComboBox column, when grid data is loaded and the data in the grid column bound to the combobox value cannot be validated, a data error event is raised.
This can be tricky because the comboBox value is bound to a column in the DGV that may not be the same column in which the combobox itself appears (e.g., it may be a hidden column or another column that reflects the combobox value).
Validation can fail due to dev errors such as type mismatch, of course, but a common reason for failure is a scenario in which the dropdown data source is fixed (non-editable and does not allow adding new rows) and the datasource has delivered data in the value column that does not match any existing entry in the dropdown. This may occur, for example, if some of the data was originally entered as free-text and was misspelled, or if data is round-tripping to another system that changes white space or capitalization. It may occur if you change the list feeding the dropdown.
In this scenario, catching and ignoring (or preferably logging) the event will cause the value that triggered the error to be replaced with the default value from the combobox (usually the first row of the dropdown). If this is the desired handling, swallow away!
I wasn't able to find another event that allowed detection and correction to be carried out before the error event fires. In particular, the CellValidating event does not fire in this scenario.
Upvotes: 0
Reputation: 17
I used all the solution above but none of them worked, so I tried to override the DataError event
and it works very well without any problem:
private void dgv_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
//do nothing
}
Upvotes: 0
Reputation: 53
yeah the solution is to make datagridviewcombobox cell value the same you are getting in the code behind.
if i want to show typeof(int) value , i must set property of the datagridviewcombobox cell like:
this.ComboboxCellcolumnName.ValueType = typeof(int);
the value type that you got(e.g int) should be the same you want to show in the combobox cell (int).
Upvotes: 2
Reputation: 686
I just had a similar experience with one of my datagridviews: DataError was getting thrown non-stop... It eventually turned out to be because the id in the combobox DataSource was of a different type (bigint) than the column that referenced it (int)...
Upvotes: 0
Reputation: 237
If, however, you wanted to revert back to your combo box column, you would need to set some special handling to set it up.
You can refer to the MSDN article here, or this example below:
MSDN: Binding Enums to DataGridViews
InitializeComponent();
// special setup for enum column
DataGridViewComboBoxColumn stateColumn = dgLedger.Columns[0] as DataGridViewComboBoxColumn;
if (stateColumn != null)
{
stateColumn.DataSource = Enum.GetValues(typeof(TransactionState));
}
_ledger = new BindingList<LedgerItem>();
dgLedger.DataSource = _ledger;
Upvotes: 0
Reputation: 81620
It looks like your DataGridViewTextBoxColumn at some point was a DataGridViewComboBoxColumn, because you have ComboBox properties that do not belong to a TextBox column.
The DataGridViewTextBoxColumn does not have:
.DataSource = this.AuthorityTypeBindingSource;
.DisplayMember = "Description";
.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
.MaxDropDownItems = 100;
.ValueMember = "Value";
I can only guess editing the designer file by hand can cause this.
Upvotes: 0