Solved

Populating a datagrid from the filesystem.

Posted on 2006-07-20
3
203 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now