将关联的DataGridView行返回到其对应的Datatable行

时间:2017-06-30 20:39:28

标签: vb.net

重新格式化这个问题,因为似乎没有人理解它。我有一个名为dtCSV的数据表。我正在通过dtCSV搜索特定值,我能够找到我需要的行(我不需要这部分的帮助)。 dtCSV也绑定到datagridview。 (这是我需要帮助的)我如何获取我在搜索中找到的行,并在datagridview控件中找到它的相应行,以便我可以突出显示整行?感谢

Function GetDebitorsString(ByVal Name As String, ByVal RowName As String) As String
    Dim dt As New DataTable

    If System.IO.File.Exists("data\debitors\debitors.xml") Then
        dt.ReadXml("data\debitors\debitors.xml")
    Else
        MsgBox("File data\debitors\debitors.xml was not found.", MsgBoxStyle.Information, "Warning")
        Return String.Empty
    End If


    Dim FoundRow() As DataRow = dt.Select("Name='" & Name & "'")
    If FoundRow IsNot Nothing Then
        Return FoundRow(0).Item(RowName).ToString
    Else
        Return String.Empty
    End If
End Function

Private Sub btnScan_Click(sender As Object, e As EventArgs) Handles btnScan.Click
    'looks through all the bills in the CSV and see's if they match with the inputted bills. If they match the cleared date is inputted into the cleared column and the row is turned green in the csv dgv

    For Each row In dt.Rows 'loop through each of the bills listed looking for a matching bank statement with the same amount paid
        If GetDebitorsString(row("Name").ToString, "Search") IsNot String.Empty Then 'find the search string for the debitor
            Dim FoundRow() As DataRow = dtCSV.Select("Description Like '*" & GetDebitorsString(row("Name").ToString, "Search") & "*'") 'try to see if any bank statements match the search string
            If FoundRow.Count >= 0 Then
                'item was found 
                If row("Amount Paid").ToString IsNot String.Empty Then 'ignore bills with no amounts typed in 
                    If Convert.ToDouble(row("Amount Paid")) = Convert.ToDouble(FoundRow(0).Item("Debit")) Then 'if the amount listed in the bill matches the amount in the bank statement 
                        row("Cleared") = FoundRow(0).Item("Date").ToString 'set the date it cleared in the cleared cell

                    End If

                End If

            End If
        End If
    Next

1 个答案:

答案 0 :(得分:0)

如果你知道 searchKey 说明名称和DGV中名称的列号,只需使用for循环。

  

示例:

    Dim key = "Andrew"
    Dim col_Name = 5
    For Each r As DataGridViewRow In dgvCSV.Rows
        If r.Cells(col_Name).Value = key Then
            Return r.Index
        End If
    Next
相关问题