Link to home
Create AccountLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

Help with forcing focus on control until data is entered propoerly.

Hi,

I'm using the code below to make sure I don't have duplicate data, problem is the user can still continue after ignoring the error message. How do I force the focus on the control when I have duplicate rows?

 Private Sub C1NSN_AfterColUpdate(sender As Object, e As C1.Win.C1TrueDBGrid.ColEventArgs) Handles C1NSN.AfterColUpdate
        Dim col As Integer = e.ColIndex
        Dim s As String
        ' sa = 0
        s = C1NSN(C1NSN.Row, e.ColIndex).ToString()
        If (s.Length < C1NSN.Columns(C1NSN.Columns.Count - 1).EditMask.Length - 3) Then
            MsgBox("Data must be in following format: xxxx-xx-xxx-xxxx")
        End If

        If e.ColIndex = 0 Then
            Dim nval As String = C1NSN.Columns(C1NSN.Col).CellValue(C1NSN.Row)
            For r = 0 To C1NSN.RowCount - 1
                If r <> C1NSN.Row Then
                    If nval = NSN.Tables(0).Rows(r)(C1NSN.Col) Then
                        MsgBox("You entered a duplicate value. Modify data entered or slect its row and Press [Delete]")
                        Exit For
                    End If
                End If
                nval = nval - 1
            Next
            nval = ""
        End If
    End Sub

Thanks,

Victor
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

If you use BeforeColUpdate event instead, you can cancel it

http://helpcentral.componentone.com/docs/c1truedbgrid/c1truedbgridbeforecolupdateevent.htm
Avatar of Victor  Charles

ASKER

Hi,

I using the code below in BeforeColumnUpdate event to check for duplicate rows but getting error nessage:

Conversion from type 'DBNull' to type 'String' is not valid.

On line

Dim nval As String = C1NSN.Columns(C1NSN.Col).CellValue(C1NSN.Row)

How do I fix this error?

Private Sub C1NSN_BeforeColUpdate(sender As Object, e As C1.Win.C1TrueDBGrid.BeforeColUpdateEventArgs) Handles C1NSN.BeforeColUpdate
         If e.ColIndex = 0 Then
            Dim nval As String = C1NSN.Columns(C1NSN.Col).CellValue(C1NSN.Row)
            For r = 0 To C1NSN.RowCount - 1
                If r <> C1NSN.Row Then
                    If nval = NSN.Tables(0).Rows(r)(C1NSN.Col) Then
                        MsgBox("You entered a duplicate value. Modify data entered or slect its row and Press [Delete]")
                        e.Cancel = True
                        Exit For
                    End If
                End If
            Next
            nval = ""
        End If
       End Sub
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Hi,

Thanks for the code, I tried the following approach but it did not work wne I entered duplicates (Message box with error message did not appear)

 If e.ColIndex = 0 Then
            If Not IsDBNull(C1NSN.Columns(C1NSN.Col).CellValue(C1NSN.Row)) Then
                Dim nval As String = C1NSN.Columns(C1NSN.Col).CellValue(C1NSN.Row)
                For r = 0 To C1NSN.RowCount - 1
                    If r <> C1NSN.Row Then
                        If nval = NSN.Tables(0).Rows(r)(C1NSN.Col) Then
                            MsgBox("You entered a duplicate value. Modify data entered or slect its row and Press [Delete]")
                            e.Cancel = True
                            Exit For
                        End If
                    End If
                Next
                nval = ""
            End If
        End If