How to catch a DataGridView check Error writing values ​​to editable cells?

I have seen many solutions for this involving data binding, but I do not have a data source. In this case, the combined cell applies only to 1 row (other rows do not have a DataGridViewComboBoxCell).

I set DataGridViewComboCell as follows:

DataGridViewComboBoxCell cell = new DataGridViewComboBoxCell(); cell.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox; cell.Items.AddRange(items.ToArray()); // items is List<string> 

And I dynamically re-populate it later as follows:

 _cell.Items.Clear(); _cell.Items.AddRange(this.Data.ResponseOptions.Select( d => d.Description).ToArray()); //d.Description is of type string 

But then I get this nasty dialogue that reads:

The following exception occurred in the DataGridView: System.ArgumentException: DataGridViewComboBoxCell is not valid. To replace this default dialog box, handle the DataError event.

This does not help, by the way, to say that it is not "valid." Can I send an email to MS saying that Windows Forms is not valid?

I tried to capture the property of the cell elements and add rows using foreach () with a call to Add (). I am still getting a dialogue.

I also tried to blow away the whole cell every time I want to update it and recreate a new DataGridViewComboCell from scratch. I am still getting a dialogue.

I also tried manually overwriting the value of the columns (successfully when I don't have this problem). However, this did not fix it.

It seems to me that this dialog appears when I try to multiply elements in a combo cell.

Now I just destroyed the DataError method.

Any suggestions?

+6
source share
6 answers

This question is a bit old, but I ran into this, and it turned out that ThunderGR was absolutely right. When I published the DataError report, I started looking at the values ​​in the database and noticed that they really were in all CAPS. Just changed my string values ​​to all caps and no longer got the error.

Here is a way to handle a DataError, although I would prefer not to fire it first.

  private void dataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e) { try { if (e.Exception.Message == "DataGridViewComboBoxCell value is not valid.") { object value = dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value; if (!((DataGridViewComboBoxColumn)dataGridView.Columns[e.ColumnIndex]).Items.Contains(value)) { ((DataGridViewComboBoxColumn)dataGridView.Columns[e.ColumnIndex]).Items.Add(value); } } throw e.Exception; } catch (Exception ex) { bool rethrow = ExceptionPolicy.HandleException(ex, "UI Policy"); if (rethrow) { MessageBox.Show(string.Format(@"Failed to bind ComboBox. " + "Please contact support with this message:" + "\n\n" + ex.Message)); } } } 

Thus, the value will be added to the collection of elements, but it will still throw an exception where it can be registered. I used the mailbox because I still think it is important that the user has an error message and contact support so that it is fixed. Hope this helps someone!

+3
source
  • Use WPF.

  • Bind the combobox column in the DataGrid to the list.

  • Sing a song - you will want it after you understand how easy it is to implement in WPF!

+1
source

Today I came across the same exception, I spend a lot of time figuring out the problem. It turned out that I had an event handler when a new row was created for the datagridview. In this event handler method, I incorrectly set the value of the combobox cell to a value that does not exist in the collection of elements of the combox field.

So, in short, the thrown exception showed the correct message. I just needed to find exactly where the wrong value was entered.

+1
source

Instead of adding to DataGridViewComboBoxColumn.Items you need to set DataGridViewComboBoxColumn.DataSource to the ResponseOptions list and set DataGridViewComboBoxColumn.ValueType = typeof (ResponseOption).

+1
source

I had the same problem and decided to add it to the list that I assign to the data source to the element corresponding to the empty element in the combo box.

For instance,

 var list = controller.GetAllMovementKind(); list.Add(new Model.MovementKind { Code = 0, Name = String.Empty }); movementKindBindingSource.DataSource = list; 

The GetAllMovementKind method returns a List<Model.MovementKind>

I hope this helps you.

0
source

To manage and verify editable data in a DataGridView, you manually add the following event:

 myDataGridView.DataError += myDataGridView_DataError; 

and implement the event this way:

 private void dataGridApprovazioni_DataError(object sender, DataGridViewDataErrorEventArgs e) { DataGridView dataGridView = (DataGridView)sender; dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = DateTime.MinValue; MessageBox.Show("Invalid Date Format", "System Info", MessageBoxButtons.OK, MessageBoxIcon.Error); } 

this example controls the dateTime fiel control in a special colum index.

0
source

Source: https://habr.com/ru/post/956444/


All Articles