2013-04-11 65 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

相關問題