0

ユーザーが間違った入力を提供した場合に、ユーザーが datagridviewcell を終了できないようにする方法を見つけようとしています。問題の datagridview 列について、ユーザーがゼロや空白のフィールドを含む負の数を入力できないようにしたい。現在、この入力を防ぎ、適切なメッセージを表示することができますが、メッセージが表示された後にユーザーフォームがセルを出るのを防ぐ方法がわかりません。私のコードを以下に示します。誰かがこの問題に対する提案や回答を持っている場合は、あなたの助けを大いに感謝します.

 Private Sub DataGridView1_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
    If (e.ColumnIndex = 2) Then
        Dim cellData = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
        If cellData Is Nothing OrElse IsDBNull(cellData) OrElse cellData.ToString = String.Empty Then
            MessageBox.Show("Cannot Be Empty")
            e.Cancel = True
        ElseIf cellData <= 0 Then
            MessageBox.Show("Negative Numbers and Zero Not Allowed")
            e.Cancel = True
            Exit Sub



        End If

    End If
End Sub
4

3 に答える 3

1

それはまたあなたです;-)イベントをSendKeys使ってみてください。CellEndEditこれは完全な解決策ではありませんが、少なくとも仕事は完了します (うまくいけば)。以下のコードを参照してください。

Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit

    If (e.ColumnIndex = 0) Then 'checking value for column 1 only
        Dim cellValue As Integer
        If (Int32.TryParse(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value, cellValue)) Then
            If cellValue < 0 Then
                MessageBox.Show("Negative Numbers Not Allowed") 'This prevents negative numbers
                SendKeys.Send("+{Tab}")
            End If
        Else
            Dim testValue As String = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
            If (String.IsNullOrEmpty(testValue)) Then
                MessageBox.Show("Name Cannot Be Empty")
                SendKeys.Send("+{Tab}")
            End If
        End If

    End If


End Sub
于 2013-07-02T01:38:13.367 に答える
1

この方法を試してください..

Private Sub DataGridView1_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating    

    If (e.ColumnIndex = 2) Then
        Dim cellData = e.FormattedValue 

        If cellData Is Nothing OrElse IsDBNull(cellData) OrElse cellData.ToString = String.Empty Then
            MessageBox.Show("Cannot Be Empty")
            e.Cancel = True
        Else
            If val(cellData) <= 0 Then
               MessageBox.Show("Negative Numbers and Zero Not Allowed")
               e.Cancel = True
               Exit Sub
            Endif    

        End If

    End If
End Sub
于 2013-07-02T00:47:18.803 に答える
0

DataGridView.CellValidatingイベントを使用して、次のように設定しe.Cancel = trueます: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvalidating.aspx

于 2013-07-01T19:16:13.280 に答える