简体   繁体   English

验证DataGridView 1中的特定单元格

[英]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 ? 将值形式的文本框传递到DataGridView时,如何验证DataGridView行的详细信息是否已经存在?

This is my function: argAccount & argPortfolio are Textbox value 这是我的功能: argAccountargPortfolio是文本框值

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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