2016-07-08 8 views
0

Ich muss ein DataGridView mit den Ergebnissen dieser Funktion füllen.Array-Ergebnis in DataGridView

Diese Funktion zählt die Zeilen jeder Datei in dem angegebenen Verzeichnis, aber ich muss speziell wissen, wie viele Zeilen pro Datei.

Also anstelle von msgbox zeigt die Gesamtzahl der Zeilen Ich möchte das Datagrid mit dieser Information füllen.

Ich habe dies zu tun, ohne viel vb.net Wissen oder was auch immer Codierung, so dass jede Hilfe wäre sehr geschätzt

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 hier ist der bearbeitete Code:

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

Sie eine Datentabelle erstellen müssen sich ändern und Ihre Ergebnisse zu jeder neuen Zeile der Datentabelle iterieren dann nur bind es an die Datagridview – htm11h

Antwort

1

Etwas So sollte das funktionieren ....

Entschuldigung, Sie müssen entscheiden, wo Sie Spalten und Zeilen hinzufügen möchten, während Sie sie in Ihren Loops verwenden, oder definieren Sie es von vorn und fügen Sie einfach Daten für jede neue Zeile hinzu hinzugefügt. Vermeide die Datentabelle nicht innerhalb einer Schleife.

 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 

Dann an die Datagridview

binden es nur
DataGridView1.DataSource = Results1 

aktualisieren, versuchen Sie dies so etwas wie ....

dies der Funktion am Anfang Hinzufügen ....

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

Dann innerhalb dieser Schleife die anderen Zeile Werte hinzufügen ...

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 

vergessen Sie nicht den Rückgabewert am Ende der Funktion ....

Return Results1 
+0

Hallo, danke für Ihre Antwort. Ich versuche es anzuwenden, aber was wäre val1 und val2? –

+0

val1 und val2 sind Ihre Ergebnisse für die Ausgabe in der Datagridview. Sie könnten den zurückgegebenen Wert von GetLineCount in Val1 und vielleicht den Dateinamen in val2 oder umgekehrt setzen. Siehe meine aktualisierte Antwort. – htm11h

+0

Ausgezeichnete Antwort, danke. Ich habe getan, wie du mir gesagt hast, dann datagridview1.datasource = restults1 hinzufügen. Und es zeigt mir keinen Fehler an, aber wie auch immer das Datagrid leer ist, laden Sie keine Daten. Entschuldigen Sie die Störung, aber können Sie sich vorstellen, was das Problem sein wird? –