Sorting DataTable DataView Asp.Net C# VB.Net

DataTable is one of the most important object in ADO.Net library. After fetching data from database we need to perform lot of functions in a DataTable. Here in this article i will show a complete example on “How to Sort DataTable or DataView by Column Name in both Ascending & Descending way”. To Sort a DataTable or DataView please foloow the below steps:

Sorting DataTable Asp.Net CSharp VB.Net

Prepare your HTML Markup:

<asp:Label ID="lbl" runat="server" Text="Default View of DataTable:" Font-Bold="true"></asp:Label>
<br />
<asp:Literal runat="server" ID="ltrl"></asp:Literal>
<asp:Button ID="cmdSort" runat="server" Text="Sort DataTable/DataView" 
    onclick="cmdSort_Click" />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Sorted DataTable:" Font-Bold="true"></asp:Label>
<br />
<asp:Literal runat="server" ID="ltrlSorted"></asp:Literal>

Prepare a DataTable to Sort:
C# Code:

    private DataTable GetDataTable()
        DataTable oTable = new DataTable("Article");

        //Add DataTable column dynamically/run time/on the fly.
        oTable.Columns.Add(new DataColumn("ID", typeof(System.Int64)));
        oTable.Columns.Add(new DataColumn("Title", typeof(System.String)));
        oTable.Columns.Add(new DataColumn("Published", typeof(System.DateTime)));

        //Add DataTable rows dynamically/run time/on the fly.
        oTable.Rows.Add(1001, "DataTable Engineering", DateTime.Now);
        oTable.Rows.Add(1002, "Event Calendar", DateTime.Now.AddDays(-1));
        oTable.Rows.Add(1003, "Master Detail Data", DateTime.Now.AddDays(-2));
        oTable.Rows.Add(1004, "Data Table Sorting", DateTime.Now);
        oTable.Rows.Add(1004, "Data View Sorting", DateTime.Now.AddDays(-5));

        return oTable;

VB.Net Code:

    Private Function GetDataTable() As DataTable
        Dim oTable As DataTable = New DataTable("Article")

        'Add DataTable column dynamically/run time/on the fly.
        oTable.Columns.Add(New DataColumn("ID", System.Type.GetType("System.Int64")))
        oTable.Columns.Add(New DataColumn("Title", System.Type.GetType("System.String")))
        oTable.Columns.Add(New DataColumn("Published", System.Type.GetType("System.DateTime")))

        'Add DataTable rows dynamically/run time/on the fly.
        oTable.Rows.Add(1001, "DataTable Engineering", DateTime.Now)
        oTable.Rows.Add(1002, "Event Calendar", DateTime.Now.AddDays(-1))
        oTable.Rows.Add(1003, "Master Detail Data", DateTime.Now.AddDays(-2))
        oTable.Rows.Add(1004, "Data Table Sorting", DateTime.Now)
        oTable.Rows.Add(1004, "Data View Sorting", DateTime.Now.AddDays(-5))

        GetDataTable = oTable
    End Function

Now Write a Print method to Print DataTable:
C# Code:

    private void PrintDataTable(DataTable oTable, Literal oltrl)
        foreach (DataRow oRecord in oTable.Rows)
            oltrl.Text += (oRecord[0].ToString() + " -> " + oRecord[1].ToString() + " -> " + oRecord[2].ToString() + "</br>");

VB.Net Code:

    Sub PrintDataTable(oTable As DataTable, oltrl As Literal)
        For Each oRecord As DataRow In oTable.Rows
            oltrl.Text += oRecord(0).ToString() + " -> " + oRecord(1).ToString() + " -> " + oRecord(2).ToString() + "</br>"
    End Sub

Now print the DataTable to View Unsorted elements:
C# Code:

    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
            PrintDataTable(GetDataTable(), ltrl);

VB.Net Code:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            PrintDataTable(GetDataTable(), ltrl)
        End If
    End Sub

Now Sort the DataTable or DataView with a Single Column:
C# Code:

    protected void cmdSort_Click(object sender, EventArgs e)
        DataView dView = new DataView(GetDataTable());
        dView.Sort = "Title ASC";
        PrintDataTable(dView.ToTable(), ltrlSorted);

Note: For Descending order use “DESC”.

VB.Net Code:

    Protected Sub cmdSort_Click(sender As Object, e As System.EventArgs)
        Dim dView As DataView = New DataView(GetDataTable())
        dView.Sort = "Title ASC"
        PrintDataTable(dView.ToTable(), ltrlSorted)
    End Sub

Note: For Descending order use “DESC”.

Now Sort the DataTable or DataView with Multiple Column:
Sorting multiple column of a DataTable is almost same as single column. Just use comma separator as DataView Sort property value like below:
C# Code:

dView.Sort = "ID DESC,Title ASC";

VB.Net Code:

dView.Sort = "ID DESC,Title ASC"

Hope now you can Sort DataTable/DataView with single & multiple column in both Ascending & Descending order.

Download Code Example C#        Download Code Example VB.Net

Posted in .Net,, C#, VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *