0
votes

I've been working on my DataError Handling on several different datagridvews in a few VB and C# projects.

These DataGridViews are bound from a table generated from a database, handle user input, and write them back to a database. If the users enter valid data, all is wonderful, but if they try something like changing the primary key to a string, the errors are plentiful.

What I have that works well, but not perfect is:

Private Sub ChemicalsDataGridViewErrorHandler(ByVal sender As Object, _
ByVal e As DataGridViewDataErrorEventArgs) Handles ChemicalsDataGridView.DataError
    e.Cancel = True
    ChemicalsDataGridView.EditingControl.Text = Nothing
    Call FillChemicalsDataGrid()  'goes back to the DB and just reloads the last valid table, writing back to DB at cell change
    Call ErrorLogWriter(e)
End Sub

This works, and clears the offending problem and get's the person back to a usable datagridview. But it also put's the cell selection back at (0,0). Is there a way I can use DataGridView.CurrentCellAddress to select the offending cell when the datagridview is reloaded?

I know I can breakdown to row and column like so:

Private Sub ChemicalsDataGridViewErrorHandler(ByVal sender As Object, _
ByVal e As DataGridViewDataErrorEventArgs) Handles ChemicalsDataGridView.DataError
    e.Cancel = True
    ChemicalsDataGridView.EditingControl.Text = Nothing
    Dim cRowInt As Int32 = ChemicalsDataGridView.CurrentCell.RowIndex
    Dim cColumnInt As Int32 = ChemicalsDataGridView.CurrentCell.ColumnIndex
    Call FillChemicalsDataGrid()  
    ChemicalsDataGridView.CurrentCell = ChemicalsDataGridView(cRowInt, cColumnInt)
    Call ErrorLogWriter(e)
End Sub

But it seems clunky to call for the row and the column separately (as someone who's never taken a programming class, I'm working on getting leaner code). Especially when I can call DataGridView.CurrentCellAddress. I tried:

Private Sub ChemicalsDataGridViewErrorHandler(ByVal sender As Object, _
ByVal e As DataGridViewDataErrorEventArgs) Handles ChemicalsDataGridView.DataError
    e.Cancel = True
    ChemicalsDataGridView.EditingControl.Text = Nothing
    Dim cCellLocation As Object = ChemicalsDataGridView.CurrentCellAddress
    Call FillChemicalsDataGrid()  
    ChemicalsDataGridView.CurrentCell = ChemicalsDataGridView(cCellLocation)
    Call ErrorLogWriter(e)
End Sub

But of course that wasn't enough arguments.

I also have been able to write the handler as a generic sub that is called, but I still haven't figured out how to have it called whenever there's an error in anyone of the different datagridviews. Is there a way to catch any DatagridView.DataError across forms in one place?

1
I've not read your question to the end but reloading a DataGridView whenever user types in invalid input is not a good idea.King King
@KingKing You know I took it out and I don't think it's needed at all for the code to work. I used it because it was in an example given to me. I thought it would help prevent future errors.Atl LED
IF you want one line: Use Dim cCellLocation As Point = ChemicalsDataGridView.CurrentCellAddress and then ChemicalsDataGridView.CurrentCell = ChemicalsDataGridView.Rows(cCellLocation.Y).Cells(cCellLocation.x)KreepN
@KreepN that's the awnser! Thanks. Here's the way I shortened it: Dim cCellLocation As Object = ChemicalsDataGridView.CurrentCellAddress and then ChemicalsDataGridView.CurrentCell = ChemicalsDataGridView(cCellLocation.X, cCellLocation.Y) ... I couldn't use Point, but late binding worked...Atl LED

1 Answers

1
votes

Just going to move my comment to an answer since it helped you out:

Private Sub ChemicalsDataGridViewErrorHandler(ByVal sender As Object, _
ByVal e As DataGridViewDataErrorEventArgs) Handles ChemicalsDataGridView.DataError
    e.Cancel = True
    ChemicalsDataGridView.EditingControl.Text = Nothing
    Dim cCellLocation As Object = ChemicalsDataGridView.CurrentCellAddress
    Call FillChemicalsDataGrid()  
    ChemicalsDataGridView.CurrentCell = ChemicalsDataGridView.Rows(cCellLocation.Y).Cells(cCellLocation.x) 
    Call ErrorLogWriter(e)
End Sub

To answer the other question regarding a single place to handle errors, you would need to add event handlers in your app and have them all point to a single method:

AddHandler MyBuChemicalsDataGridViewtton.DataError, AddressOf DGVDataError
AddHandler OtherDGV.DataError, AddressOf DGVDataError

private Sub DGVDataError(ByVal sender As Object, ByVal e As DataGridViewDataErrorEventArgs)

''dynamicly do things here for each DGV error

End Sub