Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Private
  • Views: 36
  • Last Modified:

Add column to List(ofListItem)

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
lrbrister
Asked:
lrbrister
  • 2
1 Solution
 
Ioannis ParaskevopoulosCommented:
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
 
lrbristerAuthor Commented:
jyparask
  Taking a look at this today
0
 
lrbristerAuthor Commented:
That did it.  Thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now