0

私は、いくつかの VB および C# プロジェクトで、いくつかの異なるデータグリッドビューで DataError 処理に取り組んできました。

これらの DataGridViews は、データベースから生成されたテーブルからバインドされ、ユーザー入力を処理し、データベースに書き戻します。ユーザーが有効なデータを入力した場合はすべて問題ありませんが、主キーを文字列に変更するなどの操作を試みると、エラーが大量に発生します。

私が持っているものはうまく機能しますが、完璧ではありません:

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

これは機能し、問題を解決し、ユーザーを使用可能なデータグリッド ビューに戻します。ただし、セル選択を (0,0) に戻します。DataGridView.CurrentCellAddressdatagridview がリロードされたときに問題のセルを選択するために使用できる方法はありますか?

次のように行と列に分解できることはわかっています。

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

しかし、行と列を別々に呼び出すのは不格好に思えます (プログラミングのクラスを受講したことがない私は、より効率的なコードを取得することに取り組んでいます)。特に DataGridView.CurrentCellAddress を呼び出すことができる場合。私は試した:

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

しかしもちろん、それだけでは十分な議論ではありませんでした。

また、呼び出される汎用サブとしてハンドラーを作成することもできましたが、異なるデータグリッドビューのいずれかでエラーが発生したときにハンドラーを呼び出す方法をまだ理解していません。フォーム全体で DatagridView.DataError を 1 か所でキャッチする方法はありますか?

4

1 に答える 1

1

それがあなたを助けたので、私のコメントを答えに移すつもりです:

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

エラーを処理する単一の場所に関する他の質問に答えるには、アプリにイベント ハンドラーを追加し、それらすべてが単一のメソッドを指すようにする必要があります。

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
于 2013-10-03T20:43:40.033 に答える