SolvedPrivate

Add column to List(ofListItem)

Posted on 2014-07-22
3
23 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
Comment Utility
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
Comment Utility
jyparask
  Taking a look at this today
0
 

Author Closing Comment

by:lrbrister
Comment Utility
That did it.  Thanks
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

11 Experts available now in Live!

Get 1:1 Help Now