Sort Multiple column of a DataView in Asp.net C# VB.Net

In many case studies we found that when sorting is required developers done this job in back end means running extra query in database even though he has an already disconnected record set in his hand like DataView. One can easily sort the DataView columns in both ascending and descending order. It’s very simple and for showing or displaying any type of sorted data in your report or details page you can do it without connecting to the Database again. To define the sort direction we can use ASC or DESC keyword after the column name. For multiple column we just add comma separator for each column.

Add a GridView Like Below:

<asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor="CornflowerBlue" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="White" CellPadding="5">
    </asp:GridView>

Now Sorting the DataView with Multiple Column:
C# Code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            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.AddDays(-5));
            oTable.Rows.Add(1002, "Event Calendar", DateTime.Now.AddDays(10));
            oTable.Rows.Add(1003, "Master Detail Data", DateTime.Now.AddDays(-2));

            // Convert DataTable to a DataView
            DataView DV = oTable.DefaultView;

            //Sort DataView with Multiple Column
            DV.Sort = "Title Desc, Published Asc";
            
            GridView1.DataSource = DV;
            GridView1.DataBind();
        }
    }

Note: Don’t forget to add “System.Data” namespace.

VB.Net Code:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim oTable As New DataTable("Article")

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

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

            ' Convert DataTable to a DataView
            Dim DV As DataView = oTable.DefaultView

            'Sort DataView with Multiple Column
            DV.Sort = "Title Desc, Published Asc"

            GridView1.DataSource = DV
            GridView1.DataBind()
        End If

    End Sub

Note: Don’t forget to add “System.Data” namespace.

Sorted Output:
Sorting DataView with Multiple Columns

Hope now you can sort data in the disconnected way.

Download Code Example C#        Download Code Example VB.Net

Leave a Reply

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

AlphaOmega Captcha Classica  –  Enter Security Code