SolvedPrivate

Add column to List(ofListItem)

Posted on 2014-07-22
3
25 Views
Last Modified: 2016-02-18
My code below reads a directory into a list which populates a Gridview

I need to add a "column" to the GridView that allows me to read the file extension only
i.e. jpg, xmls...etc...

    Protected Sub loadFileGrid()
        Dim filePaths() As String = Nothing
        If Session("files") = "tasks" Then
            filePaths = Directory.GetFiles(Server.MapPath(Session("taskFilePath")))
        ElseIf Session("files") = "projects" Then
            filePaths = Directory.GetFiles(Server.MapPath(Session("projectFilePath")))
        End If

        Dim files As List(Of ListItem) = New List(Of ListItem)

        For Each filePath As String In filePaths
            files.Add(New ListItem(Path.GetFileName(filePath), filePath))
        Next

        GridView1.DataSource = files
        GridView1.DataBind()
    End Sub

Open in new window

0
Comment
Question by:lrbrister
  • 2
3 Comments
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 500 total points
ID: 40213595
If you need three columns why don't you create a custom list?

Create a new class:

Public Class FileItem
    Public Property PathName As String
    Public Property Name As String
    Public Property Extention As String
    Public Sub New(aPath As String, aName As String, aExtention As String)
        PathName = aPath
        Name = aName 
        Extention = aExtention 
    End Sub
End Class

Open in new window


And then modify your code to load the grid populating a List of that class:

    Protected Sub loadFileGrid()
        Dim filePaths() As String = Nothing
        If Session("files") = "tasks" Then
            filePaths = Directory.GetFiles(Server.MapPath(Session("taskFilePath")))
        ElseIf Session("files") = "projects" Then
            filePaths = Directory.GetFiles(Server.MapPath(Session("projectFilePath")))
        End If

        Dim files As List(Of FileItem) = New List(Of FileItem)

        For Each filePath As String In filePaths
            files.Add(New FileItem(Path.GetFileName(filePath), filePath, Path.GetExtension(filePath)))
        Next

        GridView1.DataSource = files
        GridView1.DataBind()
    End Sub

Open in new window


The above is to show you a way with minimum changes in your code. I would prefer it in a different way.

First i would like the constructor of the class to be minimum. So providing only the filePath would suffice:

Public Class FileItem
    Public Property PathName As String
    Public Property Name As String
    Public Property Extention As String
    Public Sub New(aFile As String)
        PathName = Path.GetFileName(aFile)
        Name = aFile 
        Extention = Path.GetExtension(aFile)
    End Sub
End Class

Open in new window


And i would also use a bit of LINQ instead of the For Each:

    Protected Sub loadFileGrid()
        Dim filePaths() As String = Nothing
        If Session("files") = "tasks" Then
            filePaths = Directory.GetFiles(Server.MapPath(Session("taskFilePath")))
        ElseIf Session("files") = "projects" Then
            filePaths = Directory.GetFiles(Server.MapPath(Session("projectFilePath")))
        End If

        Dim files As List(Of FileItem) = filePaths.Select(Function(x) New FileItem(filePath))

        GridView1.DataSource = files
        GridView1.DataBind()
    End Sub

Open in new window


And if you would like it to be even more compact:

    Protected Sub loadFileGrid()
       GridView1.DataSource = Directory.GetFiles(IF(filess = "tasks","C:\","C:\Windows")).Select(Function(x) New FileItem(x)).ToList
        GridView1.DataBind()
    End Sub

Open in new window


I hope this helps,
Giannis
0
 

Author Comment

by:lrbrister
ID: 40214571
jyparask
  Taking a look at this today
0
 

Author Closing Comment

by:lrbrister
ID: 40220307
That did it.  Thanks
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

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