3

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;
}
Chris
  • 44,602
  • 16
  • 137
  • 156
cougar
  • 185
  • 5
  • 15

6 Answers6

2

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).

HKhan
  • 53
  • 1
  • 8
0

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.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • The box was a TextBoxColumn but then i chanaged it to a comboBoxColumn. So i believe that all the settings are correct for a combobox. Am i wrong? – cougar Jun 11 '12 at 18:19
  • @cougar There are usually two more references in the designer file to this control: One at the top of the file under `InitializeComponent` and at the end, where it is declared. Make sure both of those are changed to `DataGridViewComboBoxColumn`. Consider changing the name, too, once you have it working properly. – LarsTech Jun 11 '12 at 18:25
  • I ended up just deleting the column and re adding it. I think it might be working. Let me test it really fast before i give u the answer – cougar Jun 11 '12 at 18:28
  • Thats the other problem, it puts me on the dataerror handler but wont take me anywhere else. Ill post the code of the handler now. – cougar Jun 11 '12 at 19:29
  • @cougar Try commenting out that entire data error code and run it to see if it breaks on a code line somewhere. – LarsTech Jun 11 '12 at 19:59
  • There was still an error and it landed on my intializeCompnent Method but does not state a specific line in my method. – cougar Jun 11 '12 at 20:11
  • Why is this marked as the answer when the comments seem to say the problem persisted?? – hofnarwillie Sep 29 '14 at 13:41
  • @hofnarwillie Why would you downvote the answer? I was clearly trying to help the guy with what was obviously a local problem. Maybe the OP did figure it out but didn't leave a comment. It happens. – LarsTech Sep 29 '14 at 13:54
  • @LarsTech Nothing personal. Simply so that other answers (which possibly actually answer the question) can appear higher in the answers list. I don't believe that this answer should appear at the top, because it doesn't include a working solution. Maybe at some point cougar would post the reason why he marked it as an answer and what the final solution was, but until then there are other answers here which are more useful to viewers. – hofnarwillie Sep 30 '14 at 15:32
0

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;
James McLellan
  • 217
  • 1
  • 9
0

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)...

C.B.
  • 666
  • 3
  • 18
0

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
}
Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
mamoun
  • 17
  • 1
    This worked for me too. I added it initially to try and debug the issue, but the method doesn't even get hit and the error stopped being thrown. Weird, but it worked. – hofnarwillie Sep 30 '14 at 15:27
  • 1
    This doesn't solve the problem, just papers over it. – siride May 12 '15 at 21:43
0

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.

Craig.Feied
  • 2,617
  • 2
  • 16
  • 25