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?
reloading a DataGridView
whenever user types ininvalid input
is not a good idea. – King KingDim 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