Solved

Populating a datagrid from the filesystem.

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 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