在文本文件中查找具有特定值的所有行并将它们显示在 datagridview

     2023-02-24     21

关键词:

【中文标题】在文本文件中查找具有特定值的所有行并将它们显示在 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),因此它忽略了标题行。如果没有标题,则可以将其删除。

【讨论】:

如何在所有包含具有特定值的列的表中查找?

】如何在所有包含具有特定值的列的表中查找?【英文标题】:Howtofindinallthetablescontainingcolumnshavingparticularvalues?【发布时间】:2011-12-0909:25:03【问题描述】:我的目标是从数据库中找到所有具有特定值的给定列的表。IE。我有Tabl... 查看详情

从熊猫数据框中提取在特定列中具有特定值的所有行

】从熊猫数据框中提取在特定列中具有特定值的所有行【英文标题】:ExtractingallrowsfrompandasDataframethathavecertainvalueinaspecificcolumn【发布时间】:2013-06-2917:42:16【问题描述】:我对Python/Pandas比较陌生,并且正在努力从pd.Dataframe中提... 查看详情

在 numpy 数组中查找与所有其他行相比具有最小值的行

】在numpy数组中查找与所有其他行相比具有最小值的行【英文标题】:Findingtherowinannumpyarraythathasthesmallestvaluescomparedtoallotherrows【发布时间】:2021-11-1816:35:19【问题描述】:假设我有以下矩阵:A=[[7,5,1,2][10,1,3,8][2,2,2,3]]我需要提取... 查看详情

在 1000 万行中查找记录

...:2013-01-2520:22:36【问题描述】:我有一个逗号分隔格式的文本文件。每行有两列,每列都有整数值。喜欢12334,2343323234,45663234422,324545324543,23433143233,23433.....重复第二列中的值。我需要完成的是找到第一列中第二列具有相同值的所... 查看详情

Bash:查找包含特定字符串的文件并将它们复制到文件夹中

】Bash:查找包含特定字符串的文件并将它们复制到文件夹中【英文标题】:Bash:Findfilescontainingacertainstringandcopythemintoafolder【发布时间】:2020-02-0420:17:53【问题描述】:我想要的:在bash脚本中:查找当前目录中包含某个字符串“t... 查看详情

读取特定行中的值(可变数量的值)并将它们存储在数组中

...发布时间】:2015-03-0820:18:10【问题描述】:我有一个.txt文件,其中包含我必须使用的几千行浮点值。我不知道每条特定行有多少个值。我想将一行中的所有值存储到一个数组中。这就是我目前所拥有的全部:stringlin 查看详情

在列表中查找具有特定值的对象

】在列表中查找具有特定值的对象【英文标题】:Findobjectinlistwithspecificvalue【发布时间】:2018-08-0514:27:43【问题描述】:我有这门课:publicclassPersonpublicStringname;publicintscore;publicfloatmultiplier;publicPerson(StringnameID)name=nameID;score=0;multip... 查看详情

python以递归方式提取文件夹中所有.pdf文件中的注释和突出显示的段落,并将它们作为具有相同名称的文本文件输出(代码片段)

查看详情

在 Unix 中查找所有具有特定扩展名的文件?

】在Unix中查找所有具有特定扩展名的文件?【英文标题】:FindingallfileswithcertainextensioninUnix?【发布时间】:2012-01-2607:57:17【问题描述】:我有一个文件/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home我正在尝试查找我的硬盘驱... 查看详情

查找是不是在列表中找到具有特定属性值的元素

】查找是不是在列表中找到具有特定属性值的元素【英文标题】:Findifanelementwithaspecificpropertyvalueisfoundinalist查找是否在列表中找到具有特定属性值的元素【发布时间】:2020-06-1115:57:07【问题描述】:我正在尝试在kotlin的对象列... 查看详情

在 Python 中连接具有相同第一列值的 CSV 文件的所有行

】在Python中连接具有相同第一列值的CSV文件的所有行【英文标题】:JoiningallrowsofaCSVfilethathavethesame1stcolumnvalueinPython【发布时间】:2012-06-1709:52:31【问题描述】:我有一个类似这样的CSV文件:[\'Name1\',\'\',\'\',\'\',\'\',\'\',\'\',\'\',\'\'... 查看详情

如何使用硒在网页上查找具有特定文本的所有输入元素

】如何使用硒在网页上查找具有特定文本的所有输入元素【英文标题】:Howtofindallinputelementswithspecifictextonwebpagewithselenium【发布时间】:2022-01-0714:24:51【问题描述】:基本上我要做的是在包含特定单词的网站上找到所有输入元素... 查看详情

在具有特定键的特定值的数组中查找对象的索引[重复]

】在具有特定键的特定值的数组中查找对象的索引[重复]【英文标题】:Findindexofobjectinarraywithspecificvalueforspecifickey[duplicate]【发布时间】:2018-12-0502:26:02【问题描述】:我有一个对象,我需要在其中找到一个特定的项目索引号。... 查看详情

查找并替换所有以 # 开头的单词,并将标签文本包装在 HTML 中

】查找并替换所有以#开头的单词,并将标签文本包装在HTML中【英文标题】:FindandReplaceallwordsstartingwith#andwrapthehashtaggedtextinHTML【发布时间】:2019-04-0216:33:50【问题描述】:如果这是重复的,我深表歉意,但我似乎找不到我的答... 查看详情

如何在Python中的目录中查找具有特定文件扩展名的所有文件

】如何在Python中的目录中查找具有特定文件扩展名的所有文件【英文标题】:HowtofindallfileswithspecificfileextensionsinadirectoryinPython【发布时间】:2020-02-1416:22:01【问题描述】:假设我有一个文件夹,其中包含多个文件类型不同的文件... 查看详情

查找具有特定属性的所有类

...运行时即时找到它们使用我的库(即-我不希望某个配置文件在某处声明程序集查看和/或类名)。我正在查看AppDomain.CurrentDomain,但我对它并不太熟 查看详情

查找具有 Null 值的列并将它们写入 Pyspark 中每条记录的新列中

】查找具有Null值的列并将它们写入Pyspark中每条记录的新列中【英文标题】:FindingcolumnswithNullvaluesandwritetheminanewcolumnpereachrecordinPyspark【发布时间】:2020-11-2307:47:20【问题描述】:我有一个场景,我必须为每条记录查找具有Null值... 查看详情

如何根据查找具有特定值的实体在我的核心数据之间进行迭代

】如何根据查找具有特定值的实体在我的核心数据之间进行迭代【英文标题】:Howdoiteratebetweenmycoredatadependingtofindentitieswithaparticularvalue【发布时间】:2020-06-3015:51:28【问题描述】:我是swift的初学者。我有一个名为Task的coredata实... 查看详情