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.aspx11/27/20202,420 bytes
BinarySearch.complex.aspx11/27/20205,392 bytes
BinarySearch.simple.aspx11/27/20204,368 bytes
BindArrayDG.aspx11/27/20201,579 bytes
BindArrayDL.aspx11/27/20201,744 bytes
BindCustomClass.aspx11/27/20203,513 bytes
BindingDataGrid.aspx11/27/20203,504 bytes
blogTest.aspx8/26/20204,206 bytes
BrowserDetection.aspx11/27/20204,252 bytes
ButtonClientSide.aspx11/27/20201,596 bytes
CachedPaging.aspx11/27/20207,744 bytes
Cal_1.aspx11/27/20201,292 bytes
Cal_2.aspx11/27/20204,517 bytes
Cal_3.aspx11/27/20207,325 bytes
Calendar.aspx7/23/20201,583 bytes
CheckboxListDemo.aspx11/27/20207,555 bytes
CollapsibleRepeater.aspx11/27/20205,377 bytes
ConsumeRSS.aspx11/27/20204,869 bytes
csharpexample.aspx11/27/20203,250 bytes
customizeTemplate.aspx11/27/20204,457 bytes
CustPaging.aspx11/27/20205,901 bytes
DataTableCompute.aspx11/27/20204,026 bytes
DataTableExpression.aspx11/27/20202,714 bytes
ddlSelectedItemStyle.aspx11/27/20201,171 bytes
dgExample1.aspx11/27/20202,098 bytes
dgExample10.aspx11/27/20203,542 bytes
dgExample11.aspx11/27/20206,709 bytes
dgExample12.aspx11/27/20205,063 bytes
dgExample13.aspx11/27/20204,160 bytes
dgExample14.aspx11/27/20205,581 bytes
dgExample15.aspx11/27/20207,493 bytes
dgExample16.aspx11/27/20208,167 bytes
dgExample17.aspx11/27/202010,901 bytes
dgExample18.aspx11/27/20205,420 bytes
dgExample19.aspx11/27/20205,728 bytes
dgExample2.aspx11/27/20202,489 bytes
dgExample20.aspx11/27/20207,415 bytes
dgExample21.aspx11/27/20207,652 bytes
dgExample22.aspx11/27/20207,929 bytes
dgExample23.aspx11/27/20205,648 bytes
dgExample24.aspx11/27/20205,213 bytes
dgExample25.aspx11/27/20204,321 bytes
dgExample26.aspx7/23/20204,970 bytes
dgExample27.aspx7/23/20207,201 bytes
dgExample28.aspx7/23/20205,059 bytes
dgExample29.aspx7/23/20203,590 bytes
dgExample3.aspx11/27/20202,894 bytes
dgExample30.aspx11/27/20202,911 bytes
dgExample31.aspx11/27/20203,567 bytes
dgExample32.aspx11/27/20203,868 bytes
dgExample33.aspx11/27/20203,723 bytes
dgExample34.aspx11/27/20205,359 bytes
dgExample35.aspx11/27/20206,629 bytes
dgExample36.aspx11/27/20207,825 bytes
dgExample37.aspx11/27/20209,282 bytes
dgExample4.aspx11/27/20204,220 bytes
dgExample5.aspx11/27/20203,699 bytes
dgExample6.aspx11/27/20204,447 bytes
dgExample7.aspx11/27/20205,560 bytes
dgExample8.aspx11/27/20207,971 bytes
dgExample9.aspx11/27/20205,458 bytes
dgHighlight.aspx11/27/20208,367 bytes
dgRandomOrder.aspx7/23/20203,499 bytes
dgRandomOrder2.aspx7/23/20204,929 bytes
dgRollup.aspx11/27/202010,777 bytes
dgSubHeaders.aspx11/27/20207,481 bytes
dgSummaryDemo.aspx11/27/20202,536 bytes
DHTMLDataGrid.aspx7/23/20201,126 bytes
DispImagesFromDir.aspx11/27/20202,539 bytes
DispImagesFromDirWithResize.aspx11/27/20204,604 bytes
DispImagesFromDirWithResize2.aspx11/27/20205,752 bytes
DisplayXMLInASPNET.aspx7/23/20201,048 bytes
dlExampleRepeatColumns.aspx11/27/20203,268 bytes
DynamicDataTable.aspx11/27/20202,351 bytes
dynCtrls1.aspx11/27/20201,150 bytes
dynCtrls2.aspx11/27/20201,966 bytes
dynCtrls3.aspx11/27/20201,744 bytes
dynCtrls4.aspx11/27/20202,537 bytes
dynCtrls5.aspx11/27/20203,737 bytes
EmailControl.aspx7/23/20203,174 bytes
EmailControl2.aspx7/23/20203,573 bytes
EmailControl3.aspx7/23/20202,829 bytes
enc.aspx11/27/20205,491 bytes
ExpiringDemoA.aspx11/27/20206,112 bytes
ExpiringDemoB.aspx11/27/20207,059 bytes
FamilyTree-TreeView.aspx11/27/20203,748 bytes
FileDeptDataCaching.aspx7/23/20204,601 bytes
filesystemdisp.aspx8/26/2020976 bytes
FormatXML.aspx7/23/2020610 bytes
FreeTextBoxDemo1.aspx11/27/20202,509 bytes
FreeTextBoxDemo2.aspx11/27/20202,854 bytes
GeneratePassword.aspx11/27/20208,860 bytes
GetRenderedHTML.aspx7/23/20204,765 bytes
GoogleCache.aspx8/26/2020600 bytes
GoogleSearch.aspx11/27/20202,416 bytes
GoogleSearch.Full.aspx11/27/20206,122 bytes
hangman.aspx11/27/202014,938 bytes
HoverCalInPage.aspx7/23/20201,991 bytes
HttpWebRequestDemo1.aspx11/27/20202,683 bytes
HyperlinkInNewWindow.aspx8/11/20204,084 bytes
HyperlinkInNewWindowWithJavaScript.aspx8/11/20204,144 bytes
HyperlinkTest.aspx7/23/20204,475 bytes
IsPrime.aspx11/27/20204,810 bytes
IsPrime.Client.aspx11/27/20206,036 bytes
ListArticles.AllowDelete.aspx11/27/20205,990 bytes
ListArticles.aspx11/27/20202,560 bytes
ListArticles.Sortable.aspx11/27/20206,360 bytes
ListBoxBinding.aspx11/27/20203,405 bytes
listboxEditDG.aspx11/27/20207,763 bytes
MasterDetail.aspx11/27/20207,202 bytes
md5demo.aspx7/23/20204,165 bytes
NoncollapsibleRepeater.aspx11/27/20203,225 bytes
oneDDLInFooter.aspx11/27/202011,901 bytes
OutputCaching.aspx11/27/2020914 bytes
PagingDataGrid.aspx11/27/20206,443 bytes
PieChart.aspx7/23/20203,309 bytes
PrettyCorners.aspx7/23/20204,104 bytes
PrettyRSSDisplay.aspx11/27/20203,635 bytes
printPage.aspx11/27/20205,269 bytes
printPage_old.aspx11/27/20202,433 bytes
ProblemNotReturningDisabledControls.aspx8/11/20208,703 bytes
rc4decrypt.aspx11/27/20202,970 bytes
rc4encrypt.aspx11/27/20203,409 bytes
ReadFile.aspx11/27/20202,556 bytes
ReadTextFile.aspx11/27/20202,556 bytes
ReceivingPage.aspx11/27/20201,698 bytes
regex.aspx11/27/20208,438 bytes
RegExPCase.aspx11/27/20203,136 bytes
RFVforCB.aspx11/27/20202,634 bytes
RolloverButtonDemo.aspx7/23/20202,374 bytes
RowHighlighter.aspx11/27/20208,405 bytes
RowSelectorDemo.aspx11/27/20202,959 bytes
rsdg.Demo1.aspx11/27/20204,589 bytes
rsdg.Demo2.aspx11/27/20203,860 bytes
rsdg.Demo3.aspx11/27/20203,895 bytes
rsdg.Demo4.aspx11/27/20203,861 bytes
RssFeed.aspx8/11/20203,728 bytes
RssFeed.code.aspx11/27/20203,943 bytes
RssFeedDemo.aspx11/27/20201,454 bytes
RssFeedDemo.stylin.aspx11/27/20202,115 bytes
ScreenScrapeDemo.aspx11/27/20201,845 bytes
ScrollableDataGrid.aspx7/23/20203,976 bytes
ScrollableDataGridFixedHeader.aspx7/23/20205,219 bytes
SendEmail.aspx11/27/20205,904 bytes
SendingPage.aspx11/27/20203,333 bytes
sf.aspx7/23/2020793 bytes
ShowImage.aspx7/23/2020893 bytes
ShowThumbNail.aspx7/23/20201,661 bytes
simp.cal.aspx7/23/2020724 bytes
simpCal.aspx11/27/2020641 bytes
SimpleBoundColumn.aspx11/27/20203,078 bytes
SimpleRSSDisplay.aspx11/27/20202,028 bytes
skmMenu.aspx11/27/20201,647 bytes
skmMenu.horiz.aspx11/27/20201,999 bytes
skmMenu.stylin.aspx11/27/20201,829 bytes
StaticDataTable.aspx11/27/20202,882 bytes
StripSpaceComparison.aspx11/27/20203,050 bytes
StripWhitespace.aspx11/27/2020772 bytes
TabInterface.aspx11/27/202012,295 bytes
TamperProofURLs.A.aspx11/27/20205,284 bytes
TamperProofURLs.B.aspx11/27/20206,823 bytes
TestRepeater.aspx11/27/202010,802 bytes
thu.aspx7/23/20201,699 bytes
thumb.aspx11/27/202016,962 bytes
ToolbarDemo.aspx11/27/20201,394 bytes
TracingDemo.aspx11/27/20208,925 bytes
treeview_bound.aspx11/27/2020462 bytes
TreeViewBinding1.aspx11/27/20201,168 bytes
TreeViewXMLDemo.aspx11/27/20201,498 bytes
twoDDLsInFooter.aspx11/27/202015,425 bytes
ulRepeaterDemo.aspx11/27/20202,798 bytes
UserAgent.aspx11/27/2020960 bytes
UserInfoWriter.2.aspx7/23/20201,326 bytes
UserInfoWriter.aspx7/23/20203,393 bytes
UsingLimitColumn.aspx11/27/20203,724 bytes
UsingTemplateColumn.aspx11/27/20205,341 bytes
WriteTextFile.aspx11/27/20203,155 bytes
xmlDG.aspx11/27/202012,168 bytes
xmlRead.aspx11/27/20203,803 bytes
xmlRead2.aspx11/27/20203,803 bytes
xmlser.aspx11/27/20208,133 bytes
xmlWrite.aspx11/27/20202,869 bytes
XPathDemo.aspx7/23/2020987 bytes
XQueryDemo.aspx11/27/20202,484 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]