[英]find in a text file all rows with a specific value and display them in the datagridview
再會!
有一個.csv文本文件,格式如下:
弗雷德·史密斯 | 史密斯 | 工程師 | 21.12.2021 |
本·泰勒 | 泰勒 | 程序員 | 23.12.2021 |
比爾戴維斯 | b.戴維斯 | 程序員 | 19.12.2021 |
史蒂夫哈里斯 | 哈里斯 | 工程師 | 23.12.2021 |
湯姆沃克 | t.walker | 工程師 | 23.12.2021 |
使用以下代碼,我將文本文件中的數據顯示到 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
告訴我如何在文本文件中的 datagridview 中僅顯示那些在文本文件行中指示特定日期的員工?
例如:指定日期 - 23.12.2021 在 DataGridView 我希望顯示以下結果:
本·泰勒 | 泰勒 | 程序員 | 23.12.2021 |
史蒂夫哈里斯 | 哈里斯 | 工程師 | 23.12.2021 |
湯姆沃克 | t.walker | 工程師 | 23.12.2021 |
告訴我如何在 DataGridView 中顯示文本文件中的數據之前做出這樣的選擇? 但是,不要刪除文本文件中的這些行。
對於 j = 0 到 UBound(data)-1
循環在加入列日期之前運行,以便數據不會添加到網格中,所以我刪除了 -1。
新行的實際問題應指定行號和列號,如下所示。
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
有幾種方法可用。 如果您將數據放入實現 IBindingListView 接口的東西中,例如 DataTable,那么您可以使用 Filter 屬性。 相關文檔: BindingSource.Filter 屬性
或者,正如我在這里展示的,您可以使用 List 並使用 LINQ 創建過濾器,如下所示:
Imports System.IO
Public Class Form1
Dim userData As List(Of User) = Nothing
Dim inputDateFormat As String = "dd.MM.yyyy"
Dim displayDateFormat As String = "dd.MM.yyyy"
Public Class User
Property Name As String
Property Email As String
Property Title As String
Property MagicDate As DateTime
Sub New(name As String, email As String, title As String, magicDate As DateTime)
Me.Name = name
Me.Email = email
Me.Title = title
Me.MagicDate = magicDate
End Sub
End Class
Sub LoadData(src As String)
userData = New List(Of User)
For Each a In File.ReadLines(src)
Dim parts = a.Split("|"c)
If parts.Count = 4 Then
Dim md = DateTime.ParseExact(parts(3), inputDateFormat, Nothing)
Dim u = New User(parts(0), parts(1), parts(2), md)
userData.Add(u)
End If
Next
End Sub
Sub ShowData()
' show all the data
ShowData(Nothing)
End Sub
Sub ShowData(selectedDate? As DateTime)
If userData Is Nothing Then
Exit Sub
End If
Dim bs As New BindingSource
If selectedDate.HasValue Then
' select only the data with the desired date
bs.DataSource = userData.Where(Function(u) u.MagicDate = selectedDate.Value)
Else
' show all the data
bs.DataSource = userData
End If
DataGridView1.DataSource = bs
DataGridView1.Columns("MagicDate").DefaultCellStyle.Format = displayDateFormat
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LoadData("C:\temp\UserList.csv")
ShowData(New Date(2021, 12, 23))
End Sub
End Class
要得到:
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
為什么不做類似的事情:
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
使用 Andrew Morton 創建的“用戶”class:
Dim fileName = "C:\Bin\myFile.csv"
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim filterDate = Date.Parse("23.12.2021")
SetDataSource(filterDate)
End Sub
Private Sub SetDataSource(dateStamp As Date)
Dim data = File.ReadAllLines(fileName).Skip(1).
Select(Function(line) line.Split(",")).
Select(Function(x) New User(x)).
Where(Function(x) x.MagicDate = dateStamp)
DataGridView1.DataSource = data.ToList()
End Sub
Public Class User
Public Sub New(x() As String)
Name = x.ElementAt(0)
Email = x.ElementAt(1)
Title = x.ElementAt(2)
MagicDate = x.ElementAt(3)
End Sub
Property Name As String
Property Email As String
Property Title As String
Property MagicDate As DateTime
End Class
我在 LINQ 中添加了 Skip(1),因此它忽略了 header 行。 如果不存在 header,則可以將其刪除。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.