Search code examples
c#winformsdatagridviewcombobox

Trapping DataGrid view Error


My users want a way to do mass uploads/edits to our products database, almost as if they were using excel, where they can copy and paste in a spreadsheet. Although this makes me cringe to think about, I am not the boss so it is what it is. I am just going to dump everything to a datagrid view and let them edit it there (ugh). There are many things though that require specific values (ie Size) and I do not want them to enter in XL, XLarge, X-Large, xlarge when they should all be XLarge for example. So I need combo boxes. Easy enough, but they also want to be able to select what columns are going to be displayed, so it isn't a static table that I can just specify column types. I've managed to dynamically make the appropriate columns combobox columns and fill them with data. I fill them like so:

for (int y = 0; y < table.Rows.Count; y++)
{
    viewMassEdit.Rows.Add();
    for (int x = 0; x < table.Columns.Count; x++)
        viewMassEdit.Rows[y].Cells[x].Value = table.Rows[y][x].ToString();
}

All of the data fills correctly. No exceptions. Here's where the problem comes in. This creates a large table, and when I begin to scroll around, vertically or horizontally, these unhandled error messages periodically pop up:

enter image description here

What the crap is it talking about? It accepted the value fine when I put it there, how is it a problem now when I am navigating through the table? Furthermore, where would I go to handle this error?


Solution

  • Well, to "handle" the error - add a handler to the event DataGridView.DataError:

    void InitializeComponent() {
       // ... //
    
       this.DataGridView.DataError += this.DataGridView_DataError;
    }
    
    void DataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e) {
       e.ThrowException = false;
    }
    

    You probably want to debug that DataGridViewDataErrorEventArgs, and see what the exception, etc. is so that you can prevent it from firing in the first place.