2013-04-11 81 views

回答

1

YPU要参考使用rowCell_ID为字符串,因为它是列名在datagridivew:

Function IsInDatagridview(ByVal cell1 As String, ByVal cell2 As String, ByVal rowCell1_ID As Integer, ByVal rowCell2_ID As Integer, ByRef dgv As DataGridView) 

    Dim isFound As Boolean = False 

    For Each rw As DataGridViewRow In dgv.Rows 
     If rw.Cells("rowCell1_ID").Value.ToString = cell1 Then 
      If rw.Cells("rowCell2_ID").Value.ToString = cell2 Then 

       isFound = True 
       Return isFound 


      End If 
     End If 
    Next 

    Return isFound 

End Function 
1

有多种方法可以做到这一点。如果没有太多行,您可以检查数据源/集合或实际的datagridview本身。如果是后者,那么你可以做到这一点,像这样:如果条件满足

检查函数返回真:

Function IsInDatagridview(ByVal cell1 As String, ByVal cell2 As String, ByVal rowCell1_ID As Integer, ByVal rowCell2_ID As Integer, ByRef dgv As DataGridView) 

    Dim isFound As Boolean = False 

    For Each rw As DataGridViewRow In dgv.Rows 
     If rw.Cells(rowCell1_ID).Value.ToString = cell1 Then 
      If rw.Cells(rowCell2_ID).Value.ToString = cell2 Then 

       isFound = True 
       Return isFound 


      End If 
     End If 
    Next 

    Return isFound 

End Function 

然后用函数来显示一个消息,如果条件满足:

If (IsInDatagridview("id", "name", 0, 1, DataGridView1)) Then 

     ''// Code to display message. 
     MsgBox("Record Exists!", MsgBoxStyle.Information) 

    End If 

您可能需要更改ID为整数,但我认为它应该工作。没有测试过它。

好吧,这样做是通过遍历你指定的datagridview中的每一行'rw',检查单元格列的字符串匹配',如果找到匹配'isFound'设置为true,那么'isFound'返回。

+0

它不会工作,无论如何非常感谢你的帮助。我明白了。 – 2013-04-12 02:29:21

相关问题