Solved

Populating a datagrid from the filesystem.

Posted on 2006-07-20
3
207 Views
Last Modified: 2010-04-23
I need to populate a datagrid with the files returned from any path the user provides.  I can get this to work using a listbox, which that code is also provided below, but I can't get it to populate the datagrid.  Can anyone help me with this??

The datagrid portion is at the bottom of the code block.


    Public Sub btnImport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImport.Click
        Dim Searcher As New FileSearcher("*.*")
        Dim strFilePath As String

        strFilePath = txtPath.Text



        '***********Listbox Code*************
        ' Perform a single-directory search.
        Searcher.Search(strFilePath, False)

        ' Display results.
        Dim File As FileInfo
        For Each File In Searcher.Matches
            lbFiles.Items.Add(File.FullName)
        Next

        ' Perform a recursive directory search.
        Searcher.Search(strFilePath, True)

        ' Display results.
        For Each File In Searcher.Matches
            lbFiles.Items.Add(File.FullName)
        Next
        '*******End of Listbox Code***********



        '***********Datagrid Code*************
        dgFiles.DataSource = File.FullName
        dgFiles.DataBindings.Add(File.FullName)
        '*******End of Datagrid Code**********

        lblCount.Text = lbFiles.Items.Count

End Sub
0
Comment
Question by:scross1276
3 Comments
 
LVL 24

Accepted Solution

by:
Justin_W earned 250 total points
ID: 17148067
1. Instantiate a DataTable object.
2. As you find matching files, add a DataRow for each file to the DataTable.
3. When you are done, set the DataTable as the DataGrid's DataSource, and call DataBind().
0
 
LVL 17

Assisted Solution

by:ZeonFlash
ZeonFlash earned 250 total points
ID: 17148204
Just expanding on the method that Justin posted:

Dim dtTable As New DataTable
Dim dr As DataRow

'Add whatever columns you need here
dtTable.Columns.Add("FileName")

For Each File In Searcher.Matches
    dr = dtTable.NewRow
    dr.Item("FileName") = File.FullName
    dtTable.Rows.Add(dr)
Next

dgFiles.DataSource = dtTable
0
 

Author Comment

by:scross1276
ID: 17148286
You two are great, thanks!  I'll split the points.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question