SolvedPrivate

Add column to List(ofListItem)

Posted on 2014-07-22
3
29 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Containers and Docker for Everyone

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

729 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