2016-07-08 54 views
0

我需要用該函數的結果填充DataGridView。將數組結果導入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 
+0

你需要創建一個DataTable,並重復你的結果DataTable中的每個新行那麼就將它綁定到datagridview – htm11h

回答

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) 

然後在此循環中添加其他行值...

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 
+0

你好,謝謝你的回答。我試圖應用它,但val1和val2會是什麼? –

+0

val1和val2是輸出到datagridview的結果。您可以將返回的GetLineCount值放入Val1中,也可以將文件名放入val2中,反之亦然。看到我更新的答案。 – htm11h

+0

優秀的答案,謝謝。我按照你的說法添加了datagridview1.datasource = restults1。它並沒有向我指出任何錯誤,但是數據網格是空白的,請不要加載任何數據。抱歉打擾你,但你能想象會出現什麼問題嗎? –