简体   繁体   中英

validation for specific cell in DataGridView 1

How to do validation for DataGridView row details whether is already exists or not when pass the value form Textbox into DataGridView ?

This is my function: argAccount & argPortfolio are Textbox value

Function fnValidateAccountPortfolio(ByVal argAccount As String, ByVal 
argPortfolio As String) As Boolean
    Try
        For Each row As DataGridViewRow In Form1.DataGridView1.Rows
            If Not row.IsNewRow Then
                MessageBox.Show(row.Cells(0).Value.ToString & "  Is Already
                Exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            End If
        Next
    Catch ex As Exception
        Exit Function
    End Try
End Function

Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Handles BtnSave.Click
  If fnValidateAccountPortfolio(txtAccount.Text, txtPortfolio.Text) = False
  Then
    MessageBox.Show("Successfully Saved", "Save", MessageBoxButtons.OK,
    MessageBoxIcon.Information)
  End If
end sub

Based on the link I provided is this what you are looking for?

Function fnValidateAccountPortfolio(ByVal argAccount As String, ByVal argPortfolio As String) As Boolean

    Dim found As Boolean = False

    For Each Row As DataGridViewRow In DataGridView1.Rows
        For Each cell As DataGridViewCell In Row.Cells
            If Not (cell.Value Is Nothing) Then
               If (CStr(cell.Value).Trim() = argAccount OR CStr(cell.Value).Trim() = argPortfolio)  Then
                  MessageBox.Show(cell.Value.ToString()) ' You could show the value here just to validate but remove it later.
                  found = True
                  Exit For
               End If
            End If

        Next
        If (found) Then
            Exit For
        End If
    Next

    Return found
End Function

So, you will be calling this function like

if (fnValidateAccountPortfolio("1001","PortfolioHere")) Then
    MessageBox.Show("Entry exist already in the DataGrid!!!")
Else
    MessageBox.Show("Successfully Saved", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information)
End if

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