2

以下のコードを使用して、ユーザーが datagridview セルに文字列を入力したかどうかを確認できます。ユーザーが文字列を入力すると、「数値入力のみが許可されています」というメッセージがポップアップ表示されます。これはまさに私のコードでやりたいことです。ただし、数値が入力されたデータの列でこのコードを使用しようとすると、「エラーが発生しました。コミットを解析しています」というエラー メッセージが表示されます。ここで問題が何であるかを誰かが理解できれば、私は大歓迎です!

If (e.ColumnIndex = 3) Then 'checking numeric value for column 3 only
        Dim value As String = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
        For Each c As Char In value
            If Not Char.IsDigit(c) Then
                MessageBox.Show("Please Enter numeric Value")
                DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = 3 'This will set the defaultvalue of the datagrid cell in question to the value of "3"
                Exit Sub
            End If
        Next
    End If
4

2 に答える 2

3

このソリューションは、整数以外の値をチェックするだけでなく、データグリッドビュー全体の数値が入力されたすべての列に対しても機能します。また、ユーザーが datagridviewcell のデフォルト値を提供する場合に整数以外を入力した場合、このデフォルト値は以前の数値です。

Private Sub DataGridView1_DataError(ByVal sender As Object, _
ByVal e As DataGridViewDataErrorEventArgs) _
Handles DataGridView1.DataError

    If StrComp(e.Exception.Message, "Input string was not in a correct format.") = 0 Then
        MessageBox.Show("Please Enter a numeric Value") 
        'This will change the number back to original
        DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = " "
    End If




End Sub
于 2013-06-21T14:35:59.890 に答える
2

cellvalidating イベントでこれを行います ...

If (e.ColumnIndex = 3) Then 'checking numeric value for column 3 only

    If Not Isnumeric(e.Formatted.Value) Then

        MessageBox.Show("Please Enter numeric Value")
        DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = 3 'This will set the defaultvalue of the datagrid cell in question to the value of "3"
        Exit Sub

    End If

End If
于 2013-06-18T23:18:21.530 に答える