0
votes

I'm working with VB.NET and use a datagridview in my form. First action is loding from db. In this case are not filled all cells. I also set the active cell to the first empty cell. Then I fill in a not allowed value

I check validating on CellEndEdit --> msgbox "Wrong Value" --> set value = "" --> set begin edit Now the problem The row cursor moved even to the next row

I'm looking for a solution to let the wrong cell so long as selected until the value is correct.

2
Validation is done in the CellValidating event handler. That's it, that's all. The whole point of that event is what you are trying to achieve. - jmcilhinney
Welcome to the site! Can you post the code that's not working correctly? Can you clarify the problem? It sounds like you're not wanting a record to be added till the data is valid, based on what @jmcilhinney said. - Jimmy Smith

2 Answers

0
votes

If you want to prevent the user from leaving the current cell if they have set a wrong value, set e.Cancel = True on the CellValidating event. For example this code stops the user leaving the cell in the second column, if they didn't type a number:

    Private Sub dataGridView1_CellValidating(sender as Object, e as DataGridViewCellValidatingEventArgs e) Handles dataGridView1.CellValidating

        'set cancel to true if colindex is 1 and TryParse returned false (not a number)
        e.Cancel = (e.ColumnIndex = 1 AndAlso Not Int32.TryParse(e.FormattedValue.ToString(), Nothing)

    End Sub

To clear the input editor, you have to appreciate that you aren't editing the cell, you're typing into a textbox that is being drawn ON TOP of the place where the cell is. There is, conceptually, only one editor textbox per datagridview at any one time (and it puts it in whatever place is relevant for the current cell). The control that edits a value for the current cell is hence nothing to do with the cell, but is a feature of the grid itself, and is accessed by the dataGridView1.EditingControl. This returns a Control; it can be any kind of forms control (textbox, checkbox, datepicker etc) that edits the cell, hence why EditingControl returns a base class type of Control. Control has a .Text property and a .ResetText() method so we can clear the textbox without needing to cast it (but if we were using a date picker etc it might be that it needed casting before eg accessing its Value). This can reset the textbox:

    Private Sub dataGridView1_CellValidating(sender as Object, e as DataGridViewCellValidatingEventArgs e) Handles dataGridView1.CellValidating


        If e.ColumnIndex = 1 AndAlso Not Int32.TryParse(e.FormattedValue.ToString(), Nothing) Then
            e.Cancel = true
            dataGridView.EditingControl.ResetText()
        End If

    End Sub
0
votes

OK thanks for help. Now I have the next problem. Alfter CellValidatinr moved the row cursor to the next row.

But I have to set the current cell to next empty field, like in Form1_Load Here my short test Code

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        Dim i As Int32 = 0
        Dim emptyRow As Int32 = -1
        With Me.DataGridView1
            .Rows.Add(1, "E0", "E0")
            .Rows.Add(1, "", "E0")
            .Rows.Add(1, "E0", "E0")
            .Rows.Add(1, "", "E0")
            .Rows.Add(1, "E0", "E0")
            .Rows.Add(1, "", "E0")
        End With
        For i = 0 To Me.DataGridView1.Rows.Count - 1
            If Me.DataGridView1.Rows(i).Cells(1).FormattedValue.ToString = "" Then
                emptyRow = i
                Exit For
            End If
        Next
        If emptyRow > -1 Then
            Me.DataGridView1.CurrentCell = Me.DataGridView1(1, emptyRow)
        End If
    End Sub

    Private Sub DataGridView1_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
        e.Cancel = (e.ColumnIndex = 1 AndAlso e.FormattedValue.ToString <> "E0")
        If e.Cancel Then
            Me.txtMsg.Text = "Ungülitger Barcode"
            Me.DataGridView1.CancelEdit()
        End If
    End Sub

End Class

Which Event can I use to set the CurrentCell to the next empty Cells(1)