在文本文件中查找具有特定值的所有行并将它们显示在 datagridview
Posted
技术标签:
【中文标题】在文本文件中查找具有特定值的所有行并将它们显示在 datagridview【英文标题】:find in a text file all rows with a specific value and display them in the datagridview 【发布时间】:2022-01-24 05:29:31 【问题描述】:美好的一天!
有一个 .csv 文本文件,格式如下:
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 |
使用以下代码,我将文本文件中的数据显示到 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 中,我希望显示以下结果:
Ben Taylor | b.taylor | programmer | 23.12.2021 |
Steve Harris | s.harris | engineer | 23.12.2021 |
Tom Walker | t.walker | engineer | 23.12.2021 |
告诉我如何在 DataGridView 中显示文本文件中的数据之前做出这样的选择? 但是,不要删除文本文件中的这些行。
【问题讨论】:
【参考方案1】:对于 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
【讨论】:
【参考方案2】:有几种方法可用。如果您将数据放入实现 IBindingListView 接口的东西中,例如 DataTable,那么您可以使用 Filter 属性。相关文档:BindingSource.Filter Property
或者,正如我在这里展示的,您可以使用 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
获得:
【讨论】:
【参考方案3】: 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
【讨论】:
您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。【参考方案4】:为什么不这样做:
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
【讨论】:
【参考方案5】:使用 Andrew Morton 创建的“用户”类:
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),因此它忽略了标题行。如果没有标题,则可以将其删除。
【讨论】:
以上是关于在文本文件中查找具有特定值的所有行并将它们显示在 datagridview的主要内容,如果未能解决你的问题,请参考以下文章
查找具有 Null 值的列并将它们写入 Pyspark 中每条记录的新列中