0

将值表单Textbox传递到DataGridView时,如何验证DataGridView行详细信息是否已经存在?

这是我的功能:argAccount&argPortfolio是文本框值

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
4

1 回答 1

0

根据我提供的链接,这是您要找的吗?

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

所以,你会像这样调用这个函数

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
于 2013-05-30T02:27:07.903 回答