SolvedPrivate

Add column to List(ofListItem)

Posted on 2014-07-22
3
24 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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…

863 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

27 Experts available now in Live!

Get 1:1 Help Now