获取包含特定值的每一行

时间:2014-05-02 21:18:10

标签: .net vb.net csv

所以我目前正在尝试从包含大约53,000行5列的.csv文件中提取信息。我想要做的是在第一个'Column'中搜索某个值,例如:2004然后它将在列表框的第二列中显示相应的值,它将为每一行执行此操作。

我目前有这段代码:

Dim rowindex As String
Dim found As Boolean = False
    For Each row As DataGridViewRow In DataGridView1.Rows
        If month.Length < 2 Then
            month = "0" + month
        End If
        If row.Cells.Item("Column1").Value.ToString.Contains(year + "-" + month) Then
            rowindex = row.Index.ToString()
            found = True
            Dim actie As String = row.Cells("Column2").Value.ToString()
            MsgBox(actie)
            Exit For
        End If
    Next
    If Not found Then
        MsgBox("Item not found")
    End If

这样做是在列1中查找值,然后显示第2列中的数据。

enter image description here

所以基本上。如果第1列包含'2013',它将显示第2列中的内容,我希望它为每一行执行此操作,而不仅仅是我的代码当前正在执行的操作。我尝试为每个循环做一个,但它只显示相同的东西30次。 (我假设这是包含所需值的行数。)

对不起,如果这没有多大意义,如果没有生病,请尝试解释一下。

编辑:(已解决)

Dim rowindex As String
Dim found As Boolean = False
    For Each row As DataGridViewRow In DataGridView1.Rows
        If month.Length < 2 Then
            month = "0" + month
        End If
        If row.Cells.Item("Column1").Value.ToString.Contains(year + "-" + month) Then
            rowindex = row.Index.ToString()
            found = True
            Dim actie As String = row.Cells("Column2").Value.ToString()
            MsgBox(actie)
            Exit For
        End If
    Next
    If Not found Then
        MsgBox("Item not found")
    End If

0 个答案:

没有答案