Populate a DataTable with the files in a folder, sort using a DataView

  1. Sub binddata()
  2.     Dim strBaseFolder As String = Server.MapPath("\bulletins")
  3.  
  4.     Dim oDT As New DataTable
  5.  
  6.     'create column 1
  7.     Dim fileName As DataColumn = New DataColumn("fileName", System.Type.GetType("System.String"))
  8.     oDT.Columns.Add(fileName)
  9.  
  10.     'create column 2
  11.     Dim fullPath As DataColumn = New DataColumn("fullPath", System.Type.GetType("System.String"))
  12.     oDT.Columns.Add(fullPath)
  13.  
  14.     'create column 3
  15.     Dim relPath As DataColumn = New DataColumn("relPath", System.Type.GetType("System.String"))
  16.     oDT.Columns.Add(relPath)
  17.  
  18.     For Each s As String In Directory.GetFiles(strBaseFolder)
  19.         'create the rows
  20.         Dim oRow As DataRow
  21.         oRow = oDT.NewRow
  22.         oRow("fileName") = Path.GetFileName(s)
  23.         oRow("fullPath") = s
  24.         oRow("relPath") = "~/bulletins/" & Path.GetFileName(s)
  25.         oDT.Rows.Add(oRow)
  26.     Next s
  27.  
  28.     Dim oDV As DataView = oDT.DefaultView
  29.     oDV.Sort = "fileName desc"
  30.  
  31.     Me.Grid1.DataSource = oDV
  32.     Me.Grid1.DataBind()
  33.  
  34. End Sub

No comments:

Post a Comment