Home > Blockchain >  find in a text file all rows with a specific value and display them in the datagridview
find in a text file all rows with a specific value and display them in the datagridview

Time:12-24

Good day!

There is a .csv text file in the following format:

Fred Smith f.smith engineer 21.12.2021
Ben Taylor b.taylor programmer 23.12.2021
Bill Davis b.davis programmer 19.12.2021
Steve Harris s.harris engineer 23.12.2021
Tom Walker t.walker engineer 23.12.2021

with the following code I display data from a text file into a DataGridView:

Dim list() As String = IO.File.ReadAllLines("D:\UserList.csv", System.Text.Encoding.Default)
For i = 0 To UBound(list)
    DataGridView1.Rows.Add()
    Dim data() As String = Split(list(i), "|")
    For j = 0 To UBound(data) - 1
        DataGridView1.Item(j, i).Value = data(j)
    Next
Next

Tell me how you can display in the datagridview from a text file only those employees where a certain date is indicated in the line of the text file?

For instance: Specified the date - 23.12.2021 In the DataGridView I want the following result to be displayed:

Ben Taylor b.taylor programmer 23.12.2021
Steve Harris s.harris engineer 23.12.2021
Tom Walker t.walker engineer 23.12.2021

Tell me how you can make such a selection before displaying data from a text file in the DataGridView? However, do not delete these lines in the text file.

CodePudding user response:

For j = 0 To UBound(data)-1

loop run before Date of joining column so that data not add to grid so i removed -1.

Actual problem for the new row should specify the row and column number as below code.

Dim list() As String = IO.File.ReadAllLines("D:\UserList.csv", System.Text.Encoding.Default)
        For i = 0 To UBound(list)
            DataGridView1.Rows.Add()
            Dim data() As String = Split(list(i), "|")
            For j = 0 To UBound(data)
                'DataGridView1.Item(j, i).Value = data(j)
                DataGridView1.Rows(i).Cells(j).Value = data(j)
            Next
        Next

CodePudding user response:

There are several ways available. If you put the data into something which implements the IBindingListView interface such as a DataTable then you could use the Filter property. Documentation for that: DataGridView with only rows having the selected date

CodePudding user response:

 Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
       Dim pattern As String = "23.12.2021"
       Dim Path As String = "D:\UserList.csv"
       Dim _row As String
       Dim separator As Char = "|"

       For Each _row In File.ReadAllLines(Path, Encoding.Default)
           If _row.Contains(pattern) Then
               DataGridView1.Rows.Add(_row.Split(separator))
           End If
       Next _row
   End Sub

CodePudding user response:

Why not do something like:

Dim list() As String = IO.File.ReadAllLines("D:\UserList.csv", System.Text.Encoding.Default)
           Dim ub as Integer
               For i = 0 To UBound(list)
                Dim data() As String = Split(list(i), "|")
               ub = UBound(data)
                If data(ub) = "23.12.2021" Then
                DataGridView1.Rows.Add()
                  For j = 0 To ub - 1
                    DataGridView1.Item(j, i).Value = data(j)
                  Next
                End If
            Next
  • Related