Demo of DataGrid Listing Files in a Sortable Manner

This demo is an extension of an earlier demo that used a DataGrid to list the files in a directory. This demo allows the user to sort the output by the file name, last written date, and file size.


File NameLast Write TimeFile Size
ArraySortingDemo.aspx5/11/20172,410 bytes
BinarySearch.complex.aspx5/11/20175,239 bytes
BinarySearch.simple.aspx5/11/20174,231 bytes
BindArrayDG.aspx5/11/20171,569 bytes
BindArrayDL.aspx5/11/20171,734 bytes
BindCustomClass.aspx5/11/20173,410 bytes
BindingDataGrid.aspx5/11/20173,492 bytes
blogTest.aspx5/11/20174,203 bytes
BrowserDetection.aspx5/11/20174,162 bytes
ButtonClientSide.aspx5/11/20171,546 bytes
CachedPaging.aspx5/11/20177,734 bytes
Cal_1.aspx5/11/20171,282 bytes
Cal_2.aspx5/11/20174,507 bytes
Cal_3.aspx5/11/20177,315 bytes
Calendar.aspx5/11/20171,583 bytes
CheckboxListDemo.aspx5/11/20177,541 bytes
CollapsibleRepeater.aspx5/11/20175,367 bytes
ConsumeRSS.aspx5/11/20174,734 bytes
csharpexample.aspx5/11/20173,239 bytes
customizeTemplate.aspx5/11/20174,447 bytes
CustPaging.aspx5/11/20175,701 bytes
DataTableCompute.aspx5/11/20174,016 bytes
DataTableExpression.aspx5/11/20172,704 bytes
ddlSelectedItemStyle.aspx5/11/20171,161 bytes
dgExample1.aspx5/11/20172,088 bytes
dgExample10.aspx5/11/20173,532 bytes
dgExample11.aspx5/11/20176,699 bytes
dgExample12.aspx5/11/20175,053 bytes
dgExample13.aspx5/11/20174,150 bytes
dgExample14.aspx5/11/20175,571 bytes
dgExample15.aspx5/11/20177,483 bytes
dgExample16.aspx5/11/20178,157 bytes
dgExample17.aspx5/11/201710,891 bytes
dgExample18.aspx5/11/20175,410 bytes
dgExample19.aspx5/11/20175,718 bytes
dgExample2.aspx5/11/20172,479 bytes
dgExample20.aspx5/11/20177,405 bytes
dgExample21.aspx5/11/20177,642 bytes
dgExample22.aspx5/11/20177,919 bytes
dgExample23.aspx5/11/20175,638 bytes
dgExample24.aspx5/11/20175,203 bytes
dgExample25.aspx5/11/20174,311 bytes
dgExample26.aspx5/11/20174,970 bytes
dgExample27.aspx5/11/20177,201 bytes
dgExample28.aspx5/11/20175,059 bytes
dgExample29.aspx5/11/20173,590 bytes
dgExample3.aspx5/11/20172,884 bytes
dgExample30.aspx5/11/20172,901 bytes
dgExample31.aspx5/11/20173,557 bytes
dgExample32.aspx5/11/20173,858 bytes
dgExample33.aspx5/11/20173,713 bytes
dgExample34.aspx5/11/20175,349 bytes
dgExample35.aspx5/11/20176,619 bytes
dgExample36.aspx5/11/20177,815 bytes
dgExample37.aspx5/11/20179,272 bytes
dgExample4.aspx5/11/20174,210 bytes
dgExample5.aspx5/11/20173,689 bytes
dgExample6.aspx5/11/20174,437 bytes
dgExample7.aspx5/11/20175,550 bytes
dgExample8.aspx5/11/20177,961 bytes
dgExample9.aspx5/11/20175,448 bytes
dgHighlight.aspx5/11/20178,357 bytes
dgRandomOrder.aspx5/11/20173,499 bytes
dgRandomOrder2.aspx5/11/20174,929 bytes
dgRollup.aspx5/11/201710,767 bytes
dgSubHeaders.aspx5/11/20177,471 bytes
dgSummaryDemo.aspx5/11/20172,526 bytes
DHTMLDataGrid.aspx5/11/20171,126 bytes
DispImagesFromDir.aspx5/11/20172,529 bytes
DispImagesFromDirWithResize.aspx5/11/20174,594 bytes
DispImagesFromDirWithResize2.aspx5/11/20175,742 bytes
DisplayXMLInASPNET.aspx5/11/20171,048 bytes
dlExampleRepeatColumns.aspx5/11/20173,258 bytes
DynamicDataTable.aspx5/11/20172,340 bytes
dynCtrls1.aspx5/11/20171,140 bytes
dynCtrls2.aspx5/11/20171,956 bytes
dynCtrls3.aspx5/11/20171,734 bytes
dynCtrls4.aspx5/11/20172,527 bytes
dynCtrls5.aspx5/11/20173,727 bytes
EmailControl.aspx5/11/20173,174 bytes
EmailControl2.aspx5/11/20173,573 bytes
EmailControl3.aspx5/11/20172,829 bytes
enc.aspx5/11/20175,478 bytes
ExpiringDemoA.aspx5/11/20176,102 bytes
ExpiringDemoB.aspx5/11/20177,049 bytes
FamilyTree-TreeView.aspx5/11/20173,738 bytes
FileDeptDataCaching.aspx5/11/20174,601 bytes
filesystemdisp.aspx5/11/2017975 bytes
FormatXML.aspx5/11/2017610 bytes
FreeTextBoxDemo1.aspx5/11/20172,441 bytes
FreeTextBoxDemo2.aspx5/11/20172,780 bytes
GeneratePassword.aspx5/11/20178,849 bytes
GetRenderedHTML.aspx5/11/20174,765 bytes
GoogleCache.aspx5/11/2017599 bytes
GoogleSearch.aspx5/11/20172,406 bytes
GoogleSearch.Full.aspx5/11/20176,112 bytes
hangman.aspx5/11/201714,928 bytes
HoverCalInPage.aspx5/11/20171,991 bytes
HttpWebRequestDemo1.aspx5/11/20172,673 bytes
HyperlinkInNewWindow.aspx5/11/20174,084 bytes
HyperlinkInNewWindowWithJavaScript.aspx5/11/20174,144 bytes
HyperlinkTest.aspx5/11/20174,475 bytes
IsPrime.aspx5/11/20174,800 bytes
IsPrime.Client.aspx5/11/20176,026 bytes
ListArticles.AllowDelete.aspx5/11/20175,980 bytes
ListArticles.aspx5/11/20172,550 bytes
ListArticles.Sortable.aspx5/11/20176,350 bytes
ListBoxBinding.aspx5/11/20173,394 bytes
listboxEditDG.aspx5/11/20177,753 bytes
MasterDetail.aspx5/11/20176,995 bytes
md5demo.aspx5/11/20174,165 bytes
NoncollapsibleRepeater.aspx5/11/20173,215 bytes
oneDDLInFooter.aspx5/11/201711,891 bytes
OutputCaching.aspx5/11/2017904 bytes
PagingDataGrid.aspx5/11/20176,432 bytes
PieChart.aspx5/11/20173,309 bytes
PrettyCorners.aspx5/11/20174,104 bytes
PrettyRSSDisplay.aspx5/11/20173,625 bytes
printPage.aspx5/11/20175,257 bytes
printPage_old.aspx5/11/20172,420 bytes
ProblemNotReturningDisabledControls.aspx5/11/20178,703 bytes
rc4decrypt.aspx5/11/20172,960 bytes
rc4encrypt.aspx5/11/20173,399 bytes
ReadFile.aspx5/11/20172,546 bytes
ReadTextFile.aspx5/11/20172,546 bytes
ReceivingPage.aspx5/11/20171,688 bytes
regex.aspx5/11/20178,423 bytes
RegExPCase.aspx5/11/20173,126 bytes
RFVforCB.aspx5/11/20172,623 bytes
RolloverButtonDemo.aspx5/11/20172,374 bytes
RowHighlighter.aspx5/11/20178,393 bytes
RowSelectorDemo.aspx5/11/20172,949 bytes
rsdg.Demo1.aspx5/11/20174,579 bytes
rsdg.Demo2.aspx5/11/20173,850 bytes
rsdg.Demo3.aspx5/11/20173,885 bytes
rsdg.Demo4.aspx5/11/20173,851 bytes
RssFeed.aspx5/11/20173,728 bytes
RssFeed.code.aspx5/11/20173,933 bytes
RssFeedDemo.aspx5/11/20171,444 bytes
RssFeedDemo.stylin.aspx5/11/20172,105 bytes
ScreenScrapeDemo.aspx5/11/20171,834 bytes
ScrollableDataGrid.aspx5/11/20173,976 bytes
ScrollableDataGridFixedHeader.aspx5/11/20175,219 bytes
SendEmail.aspx5/11/20175,893 bytes
SendingPage.aspx5/11/20173,323 bytes
sf.aspx5/11/2017793 bytes
ShowImage.aspx5/11/2017893 bytes
ShowThumbNail.aspx5/11/20171,661 bytes
simp.cal.aspx5/11/2017724 bytes
simpCal.aspx5/11/2017631 bytes
SimpleBoundColumn.aspx5/11/20173,068 bytes
SimpleRSSDisplay.aspx5/11/20172,018 bytes
skmMenu.aspx5/11/20171,637 bytes
skmMenu.horiz.aspx5/11/20171,989 bytes
skmMenu.stylin.aspx5/11/20171,819 bytes
StaticDataTable.aspx5/11/20172,872 bytes
StripSpaceComparison.aspx5/11/20173,040 bytes
StripWhitespace.aspx5/11/2017762 bytes
TabInterface.aspx5/11/201712,285 bytes
TamperProofURLs.A.aspx5/11/20175,274 bytes
TamperProofURLs.B.aspx5/11/20176,812 bytes
TestRepeater.aspx5/11/201710,792 bytes
thu.aspx5/11/20171,699 bytes
thumb.aspx5/11/201716,952 bytes
ToolbarDemo.aspx5/11/20171,384 bytes
TracingDemo.aspx5/11/20178,911 bytes
treeview_bound.aspx5/11/2017452 bytes
TreeViewBinding1.aspx5/11/20171,158 bytes
TreeViewXMLDemo.aspx5/11/20171,488 bytes
twoDDLsInFooter.aspx5/11/201715,415 bytes
ulRepeaterDemo.aspx5/11/20172,788 bytes
UserAgent.aspx5/11/2017950 bytes
UserInfoWriter.2.aspx5/11/20171,326 bytes
UserInfoWriter.aspx5/11/20173,393 bytes
UsingLimitColumn.aspx5/11/20173,714 bytes
UsingTemplateColumn.aspx5/11/20175,331 bytes
WriteTextFile.aspx5/11/20173,145 bytes
xmlDG.aspx5/11/201712,158 bytes
xmlRead.aspx5/11/20173,793 bytes
xmlRead2.aspx5/11/20173,793 bytes
xmlser.aspx5/11/20178,122 bytes
xmlWrite.aspx5/11/20172,859 bytes
XPathDemo.aspx5/11/2017987 bytes
XQueryDemo.aspx5/11/20172,474 bytes


Source Code
ListArticles.Sortable.aspx
<%@ Page Language="vb" AutoEventWireup="false" 
        Codebehind="ListArticles.Sortable.aspx.vb" 
        Inherits="SortableFileEntries.ListArticleSortable"%>
<form runat="server" ID="Form1">
   <asp:DataGrid runat="server" id="articleList" Font-Name="Verdana" AutoGenerateColumns="False" 
           AlternatingItemStyle-BackColor="#eeeeee" HeaderStyle-BackColor="Navy" 
           HeaderStyle-ForeColor="White" HeaderStyle-Font-Size="15pt" 
           HeaderStyle-Font-Bold="True" 
           AllowSorting="True" OnSortCommand="sortDisplay">
      <Columns>
         <asp:HyperLinkColumn DataNavigateUrlField="Name" DataTextField="Name" 
                            HeaderText="File Name" SortExpression="FileName" />
         <asp:BoundColumn DataField="LastWriteTime" HeaderText="Last Write Time" 
                            ItemStyle-HorizontalAlign="Center" DataFormatString="{0:d}" 
                            SortExpression="LastWriteTime" />
         <asp:BoundColumn DataField="Length" HeaderText="File Size" 
                            ItemStyle-HorizontalAlign="Right" 
                            DataFormatString="{0:#,### bytes}" 
                            SortExpression="Length" />
      </Columns>
   </asp:DataGrid>
</form>      
		

Code-Behind Class

Imports System.Collections
Imports System.IO

Public Class ListArticleSortable
    Inherits System.Web.UI.Page
    Protected WithEvents articleList As System.Web.UI.WebControls.DataGrid

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                 Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                 Handles MyBase.Load
        If Not Page.IsPostBack Then
            BindData(CompareByOptions.FileName)
        End If
    End Sub

    Private Sub BindData(ByVal compareMethod As CompareByOptions)
        Dim dirInfo As New DirectoryInfo(Server.MapPath(""))
        Dim fileInfoArray() As FileInfo = dirInfo.GetFiles("*.aspx")

        Array.Sort(fileInfoArray, New CompareFileInfoEntries(compareMethod))
        articleList.DataSource = fileInfoArray
        articleList.DataBind()
    End Sub

    Public Sub SortDisplay(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs)
        Select Case e.SortExpression
            Case "FileName"
                BindData(CompareByOptions.FileName)
            Case "LastWriteTime"
                BindData(CompareByOptions.LastWriteTime)
            Case "Length"
                BindData(CompareByOptions.Length)
        End Select
    End Sub

End Class

Public Enum CompareByOptions
    FileName
    LastWriteTime
    Length
End Enum


Public Class CompareFileInfoEntries
    Implements IComparer

    Private compareBy As CompareByOptions = CompareByOptions.FileName


    Public Sub New(ByVal cBy As CompareByOptions)
        compareBy = cBy
    End Sub

    Public Overridable Overloads Function Compare(ByVal file1 As Object, ByVal file2 As Object) _
                 As Integer Implements IComparer.Compare
        'Convert file1 and file2 to FileInfo entries
        Dim f1 As FileInfo = CType(file1, FileInfo)
        Dim f2 As FileInfo = CType(file2, FileInfo)

        'Compare the file names
        Select Case compareBy
            Case CompareByOptions.FileName
                Return String.Compare(f1.Name, f2.Name)
            Case CompareByOptions.LastWriteTime
                Return DateTime.Compare(f1.LastWriteTime, f2.LastWriteTime)
            Case CompareByOptions.Length
                Return f1.Length - f2.Length
        End Select
    End Function
End Class
	

[Return to the article]