简体   繁体   中英

VB Getting DataGridView to stay on same cell if validation (CellEndEdit fails

Hi I am very new to programming. Managed to make a helpful program last month and am now onto slightly bigger things- DataGridviews are hurting my head. My Datagridview is not bound to a Database. In short i have two DataGridviews on two different forms- one is essentially a Dictionary- and the other is a Data Entry Sheet (a bit like excel). The DataEntry Datagrid Cross references the Dictionary Datagrid. I have got this working however what i need to do is - if the data cell in the data entry sheet is not in the dictionary after editing then it will not go to another cell (IE. stuck in that cell until a correct dictionary value is added. I can currently make it say msgbox"not in dictionary" however my code for not allowing moving out of the cell is not working-

Here is the code

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


    Dim Row As Integer = DataGridView1.CurrentRow.Index
    Dim temp As Integer = 0

    Try
        For i As Integer = 0 To FormGeoDicLith.DataGridViewDicLith.RowCount - 1

'this code references a column in the datagrid dictionary to see if the correct value has been added to the data entry datagrid

            If DataGridView1.Rows(Row).Cells(2).Value = FormGeoDicLith.DataGridViewDicLith.Rows(i).Cells(1).Value Then
                MsgBox("Item found")
                temp = 1
            End If
        Next

        If temp = 0 Then

'this is the problem area
            DataGridView1.Rows(Row).Cells(2).Selected = True

            MsgBox("Code Not In Dictionary")

            Exit Sub

        End If
    Catch ex As Exception

    End Try
End Sub  

The problem is DataGridView1.Rows(Row).Cells(2).Selected = True although it looks like it selects the cell- then it just un-selects and I am not stuck in the cell like i want to be until a correct dictionary item is entered. Help would be very much appreciated.

You can use DataGridView.CellValidating Event that occurs when a cell loses input focus, enabling content validation.

For example:

Private Sub DataGridView1_CellValidating(sender As Object, e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) _
    Handles DataGridView1.CellValidating

    If e.FormattedValue < 0 Or e.FormattedValue > 20 Then
        MsgBox("Please specify a valid value between 0 and 20.")
        e.Cancel = True
    End If

End Sub

You can validate only particular column filtering by e.ColumnIndex .

I have made a bit of progress with-

DataGridView1.CurrentCell = DataGridView1.Rows(Row).Cells(2)

            MsgBox("Code Not In Dictionary")

            DataGridView1.BeginEdit(True)

the begin edit function does pull me back to the cell although the next cell is selected then the it is pulled back to the correct cell and put in edit mode. It takes 2 presses of the enter key instead of one after Msgbox and seems to fail if i continually put in a wrong value/ends up not selecting the cell for some reason.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM