Results 1 to 7 of 7

Thread: [RESOLVED] Datagrid validation in vb. (if statement)

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Posts
    13

    Resolved [RESOLVED] Datagrid validation in vb. (if statement)

    I have a datagrid with several columns, the first column is the release column. This column validate that the release is valid and validate that the release is in another table.

    I have two private functions that help with the validation: Private Function CheckSatus(strRelease As String) As Boolean and Private Function CheckRelease(strRelease As String) As Boolean

    What is currently happening:
    When you enter an invalid release it turns the back color yellow.

    What is expected:

    It should hit the CheckSatus(chkValue) and turn back color red and stop.

    Here is the code I currently have:

    Private Sub gridUserEntries_CellLeave(sender As Object, e As DataGridViewCellEventArgs) Handles gridUserEntries.CellLeave
    If (e.ColumnIndex = 0) Then
    Dim currCell As DataGridViewCell = gridUserEntries.CurrentCell
    Dim chkValue As String = currCell.GetEditedFormattedValue(currCell.RowIndex, DataGridViewDataErrorContexts.Display)

    If Not (chkValue.Trim = "") Then
    'Validate if release is in jobscopedb.IPJOBM table
    If CheckSatus(chkValue) Then
    currCell.Style.BackColor = Color.White
    Else
    currCell.Style.BackColor = Color.Red
    btnUpdatePPUSRFS.Enabled = False
    btnClear.Enabled = True
    End If
    'Validate if the Release Number is in the PPUSRFS TABLE
    If CheckRelease(chkValue) Then
    currCell.Style.BackColor = Color.White
    btnValidate.Enabled = True
    btnRetrieve.Enabled = True
    btnClear.Enabled = True
    Else
    currCell.Style.BackColor = Color.Yellow
    btnInsertPPUSRFS.Enabled = True
    btnValidate.Enabled = False
    btnRetrieve.Enabled = False
    End If
    Else
    currCell.Style.BackColor = Color.White
    End If

    End If

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Datagrid validation in vb. (if statement)

    Code:
    If CheckSatus(chkValue) Then
        currCell.Style.BackColor = Color.White
    Else
        currCell.Style.BackColor = Color.Red
        btnUpdatePPUSRFS.Enabled = False
        btnClear.Enabled = True
        Return
    End If
    'Validate if the Release Number is in the PPUSRFS TABLE
    If CheckRelease(chkValue) Then
        currCell.Style.BackColor = Color.White
        btnValidate.Enabled = True
        btnRetrieve.Enabled = True
        btnClear.Enabled = True
    Else
        currCell.Style.BackColor = Color.Yellow
        btnInsertPPUSRFS.Enabled = True
        btnValidate.Enabled = False
        btnRetrieve.Enabled = False
    End If

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Datagrid validation in vb. (if statement)

    When you post code, select the code block and click the # button in the post editor window ^
    Your code formatting will be retained, and we can all read it easier...

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Posts
    13

    Re: Datagrid validation in vb. (if statement)

    Quote Originally Posted by .paul. View Post
    Code:
    If CheckSatus(chkValue) Then
        currCell.Style.BackColor = Color.White
    Else
        currCell.Style.BackColor = Color.Red
        btnUpdatePPUSRFS.Enabled = False
        btnClear.Enabled = True
        Return
    End If
    'Validate if the Release Number is in the PPUSRFS TABLE
    If CheckRelease(chkValue) Then
        currCell.Style.BackColor = Color.White
        btnValidate.Enabled = True
        btnRetrieve.Enabled = True
        btnClear.Enabled = True
    Else
        currCell.Style.BackColor = Color.Yellow
        btnInsertPPUSRFS.Enabled = True
        btnValidate.Enabled = False
        btnRetrieve.Enabled = False
    End If


    Thanks so much Paul that worked.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Datagrid validation in vb. (if statement)

    If your issue is resolved, please use the Thread Tools menu to mark the thread Resolved. That way, we all know that you don't need any more help without opening the thread and reading the whole thing.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Posts
    13

    Re: Datagrid validation in vb. (if statement)

    So now I have another question:

    I have a button that retrieves data and place the information in data grid, its currently working but I need to add a message box to it. If the back color in the grid is red when the button is click a message box appear to "Correct all red fields before continuing. I have tried to insert message boxes to test but in the for each loop the message box appeared for each red back color cell and that not what I need it to do.

    Here is the code:
    Private Sub Retrieve()

    On Error GoTo errH

    Dim con As OdbcConnection

    Dim cmd As OdbcCommand

    Dim dr As OdbcDataReader
    Dim strItem1 As String
    Dim strItem2 As String

    Dim strOdbc As String = ""
    Dim hasRows As Boolean = False
    Dim strItem3 As String
    Dim strRelease As String

    Dim sqlStr As String


    Dim strDsn, strSystem As String
    strDsn = ComboBox1.Text
    strSystem = txtSystem.Text


    strOdbc = "{iSeries As ODBC Driver};System=" + strSystem + ";Dsn=" + strDsn + ";Uid=TEST!;Pwd=Test;"
    con = New OdbcConnection(strOdbc)
    con.Open()
    Using con

    For Each dgvRow As DataGridViewRow In gridUserEntries.Rows
    If (Not IsNothing(dgvRow.Cells(0))) Then
    If (Not (IsNothing(dgvRow.Cells(0).Value))) Then
    strRelease = dgvRow.Cells(0).GetEditedFormattedValue(dgvRow.Cells(0).RowIndex, DataGridViewDataErrorContexts.Display)
    sqlStr = "Select * from jobscopedb.ppusrfs where search_key_uf= '" + strRelease + "'"
    cmd = New OdbcCommand(sqlStr, con)
    'con.Open()
    dr = cmd.ExecuteReader()
    hasRows = dr.HasRows
    While (hasRows And dr.Read)
    strRelease = dr.Item(0).ToString
    strItem2 = dr.Item(2).ToString
    strItem3 = dr.Item(3).ToString

    If (strItem2.Contains("27 PRODWK")) Then
    dgvRow.Cells(1).Value = dr.Item(3).ToString.Trim
    End If
    If (strItem2.Contains("28 SHIPMON")) Then
    dgvRow.Cells(2).Value = dr.Item(3).ToString.Trim
    End If
    If (strItem2.Contains("30 %COMPL")) Then
    dgvRow.Cells(3).Value = dr.Item(3).ToString.Trim
    End If
    If (strItem2.Contains("31 TGTSHIP")) Then
    dgvRow.Cells(4).Value = dr.Item(3).ToString.Trim
    End If
    If (strItem2.Contains("70 SCHPROD")) Then
    dgvRow.Cells(5).Value = dr.Item(3).ToString.Trim
    End If
    If (strItem2.Contains("81 AB%")) Then
    dgvRow.Cells(6).Value = dr.Item(4).ToString.Trim
    End If
    If (strItem2.Contains("82 ARM%")) Then
    dgvRow.Cells(7).Value = dr.Item(4).ToString.Trim
    End If
    If (strItem2.Contains("83 SHAFT%")) Then
    dgvRow.Cells(8).Value = dr.Item(4).ToString.Trim
    End If
    If (strItem2.Contains("84 FIT% ")) Then
    dgvRow.Cells(9).Value = dr.Item(4).ToString.Trim
    End If
    If (strItem2.Contains("85 HDW%")) Then
    dgvRow.Cells(10).Value = dr.Item(4).ToString.Trim
    End If
    If (strItem2.Contains("86 FIN% ")) Then
    dgvRow.Cells(11).Value = dr.Item(4).ToString.Trim
    End If
    If (strItem2.Contains("87 WELDCMP")) Then
    dgvRow.Cells(12).Value = dr.Item(3).ToString.Trim
    End If

    End While
    cmd.Dispose()
    End If
    End If
    Next
    End Using

    Exit Sub

    errH:

    MsgBox(Err.Description)

    con = Nothing

    End Sub
    Last edited by msboddie; Dec 11th, 2020 at 10:11 PM.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Datagrid validation in vb. (if statement)

    Quote Originally Posted by msboddie View Post
    So now I have another question
    Then create a new thread and provide the information specific to that question. If the issue you originally posted about is resolved then mark this thread Resolved using the Thread Tools menu.

    Also, .paul. asked yo to format your code and even told you how in post #3 and yet now you've posted a big wad of code without formatting it again. If you'd like us to volunteer our time to help, please do what you can to help us help you. Unformatted code is much harder to read so it wastes our time trying to read it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width