【问题标题】:Array result into DataGridView数组结果到 DataGridView
【发布时间】:2016-07-08 20:09:29
【问题描述】:

我需要用该函数的结果填充 DataGridView。

此函数计算指定目录中每个文件的行数,但我需要具体知道每个文件有多少行。

因此,我想用该信息填充数据网格,而不是显示总行数的 msgbox。

我在没有太多 vb.net 知识或编码的情况下一直在做这件事,所以任何帮助都将不胜感激

Imports System
Imports System.Collections
Imports System.Linq
Imports System.IO
Imports System.IO.StreamReader
Imports System.IO.FileInfo
Imports System.IO.DirectoryInfo

Public Class Form1

    '/ this function returns the count of code lines
    'FileNames holds the names of files in the project directories
    Protected FileNames As New ArrayList(200)

    ' it returns filenames in the project
    Public ReadOnly Property FilesInProject() As ArrayList
        Get
            Return FileNames
        End Get
    End Property

    ' this function returns the count of code lines
    Public Function GetLineCount() As Integer

        Dim LineCount As Integer = 0

        ' this array holds file types, you can add more file types if you want
        Dim myFileArray As [String]() = New [String](6) {"*.txt", "*.doc", "*.docx", "*.odt", "*.pdf", "*.rtf", _
            "*.csv"}

        ' this array holds directories where your project files resides
        Dim myDirectoryArray As [String]() = New [String](0) {"c:\test\"}

        'this loops directories
        For Each sd As [String] In myDirectoryArray
            Dim dir As New DirectoryInfo(sd)

            ' this loops file types
            For Each sFileType As [String] In myFileArray

                ' this loops files
                For Each file__1 As FileInfo In dir.GetFiles(sFileType)

                    ' add the file name to FileNames ArrayList
                    FileNames.Add(file__1.FullName)

                    ' open files for streamreader
                    Dim sr As StreamReader = File.OpenText(file__1.FullName)

                    'loop until the end
                    While sr.ReadLine() IsNot Nothing
                        LineCount += 1
                    End While
                    'close the streamreader
                    sr.Close()

                Next
            Next
        Next
        Return LineCount

    End Function

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        Try
            MsgBox(GetLineCount)
            ' i want to put here something like datagridview1.datasource = getlinecount
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub DataGridView1_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    End Sub

End Class

@htm11h 这里是编辑后的代码:

Imports System
Imports System.Collections
Imports System.Linq
Imports System.IO
Imports System.IO.StreamReader
Imports System.IO.FileInfo
Imports System.IO.DirectoryInfo

Public Class Form1

    Dim results1 As New DataTable
    '/ this function returns the count of code lines
    'FileNames holds the names of files in the project directories
    Public FileNames As New ArrayList(200)

    ' it returns filenames in the project
    Public ReadOnly Property FilesInProject() As ArrayList
        Get
            Return FileNames
        End Get
    End Property

    ' this function returns the count of code lines
    Public Function GetLineCount() As Integer

        Dim LineCount As Integer = 0

        ' this array holds file types, you can add more file types if you want
        Dim myFileArray As [String]() = New [String](6) {"*.txt", "*.doc", "*.docx", "*.odt", "*.pdf", "*.rtf", _
            "*.csv"}

        ' this array holds directories where your project files resides
        Dim myDirectoryArray As [String]() = New [String](0) {"c:\test\"}

        'this loops directories
        For Each sd As [String] In myDirectoryArray
            Dim dir As New DirectoryInfo(sd)

            ' this loops file types
            For Each sFileType As [String] In myFileArray

                ' this loops files
                For Each file__1 As FileInfo In dir.GetFiles(sFileType)

                    ' add the file name to FileNames ArrayList
                    FileNames.Add(file__1.FullName)

                    ' open files for streamreader
                    Dim sr As StreamReader = File.OpenText(file__1.FullName)

                    'loop until the end
                    While sr.ReadLine() IsNot Nothing
                        LineCount += 1
                    End While
                    'close the streamreader
                    sr.Close()

                    results1.Rows.Add()
                    results1.Rows(0).Item(0) = "Filename: " & file__1.FullName
                    results1.Rows(0).Item(1) = "Count: " & LineCount

                Next
            Next
        Next

        Return LineCount



    End Function


    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        Try

            DataGridView1.DataSource = GetLineCount()

        Catch ex As Exception

            MsgBox(ex.Message)

        End Try

    End Sub

    Private Sub DataGridView1_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    End Sub

End Class

【问题讨论】:

  • 您需要创建一个数据表并将结果迭代到数据表的每个新行,然后将其绑定到 datagridview

标签: vb.net


【解决方案1】:

这样的事情应该可以工作....

抱歉,当您在循环中使用列和行时,您需要决定在哪里添加它们,或者预先定义它并在添加每个新行时添加数据。显然不要在循环中对数据表进行调暗。

        Dim Results1 As New DataTable

        Results1.TableName = "output"
        Results1.Columns.Add(0)
        Results1.Columns.Add(1)
        Results1.Rows.Add()
        Results1.Rows(0).Item(0) = val1
        Results1.Rows(0).Item(1) = val2

        Return Results1

然后将其绑定到 DataGridView

DataGridView1.DataSource = Results1

更新,试试这样的......

在函数的开头加上这个......

Public Function GetLineCount() As DataTable 
      Results1.Columns.Add(0) 
      Results1.Columns.Add(1) 

然后在这个循环中添加其他 Row 值...

For Each file__1 As FileInfo In dir.GetFiles(sFileType)

    ' add the file name to FileNames ArrayList
    FileNames.Add(file__1.FullName)

    ' open files for streamreader
    Dim sr As StreamReader = File.OpenText(file__1.FullName)

    'loop until the end
     While sr.ReadLine() IsNot Nothing
           LineCount += 1
     End While

     'close the streamreader
     sr.Close()

     Results1.Rows.Add()
     Results1.Rows(0).Item(0) = "Filename: " & file__1.FullName
     Results1.Rows(0).Item(1) = "Count: "  & LineCount 

Next

不要忘记在函数末尾更改返回值......

Return Results1

【讨论】:

  • 您好,谢谢您的回答。我正在尝试应用它,但 val1 和 val2 会是什么?
  • val1 和 val2 是您要输出到 datagridview 的结果。您可以将 GetLineCount 的返回值放在 Val1 中,也可以将文件名放在 val2 中,反之亦然。请参阅我的更新答案。
  • 很好的答案,谢谢。我按照你告诉我的做了,然后添加 datagridview1.datasource = restults1。它并没有向我表明任何错误,但是数据网格是空白的,不要加载任何数据。很抱歉打扰您,但您能想象会是什么问题吗?
  • 您首先定义了结果集数据表,然后发布您更新的代码。
  • 是的,我会用新代码编辑原始问题
猜你喜欢
  • 2019-08-06
  • 1970-01-01
  • 2016-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-01
  • 2021-04-29
  • 1970-01-01
相关资源
最近更新 更多