c# - How to sort a DataView

DataView Sort Property
.Net framework's DataView represents a databindable, customized view of a DataTable for sorting, filtering, searching, editing and navigation. DataView class exists in System.Data namespace.

DataView.Sort property allow us to get or set the sort column or columns and sort order for the DataView. DataView Sort property value data type is System.String. this value represents a string that contains the column name followed by 'ASC' ascending or 'DESC' (descending).

If we want to sort DataView by multiple columns, we need to separate multiple columns by commas. by default, columns are sorted ascending order. we can implements DataView Sort property value as "BookName Desc" for single column sorting or "Country ASC, City DESC" for multiple columns sorting.

The following ado.net c# example code demonstrate us how can we sort DataView data programmatically at run time in an asp.net application.
DataViewSortProperty.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Data" %>

<!DOCTYPE html>
<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.TableName = "Books";

        DataColumn dc1 = new DataColumn();
        dc1.ColumnName = "BookID";
        dc1.DataType = typeof(int);
        dc1.AllowDBNull = false;
        dc1.Unique = true;

        DataColumn dc2 = new DataColumn();
        dc2.ColumnName = "Category";
        dc2.DataType = typeof(string);

        DataColumn dc3 = new DataColumn();
        dc3.ColumnName = "BookName";
        dc3.DataType = typeof(string);

        DataColumn dc4 = new DataColumn();
        dc4.ColumnName = "Author";
        dc4.DataType = typeof(string);

        dt.Columns.AddRange(new DataColumn[] { dc1, dc2, dc3, dc4 });

        dt.Rows.Add(new object[] { 1, "Java", "Google App Engine Java and GWT Application Development", "Daniel Guermeur, Amy Unruh" });
        dt.Rows.Add(new object[] { 2, ".NET", "ASP.NET MVC 1.0 Quickly", "Maarten Balliauw" });
        dt.Rows.Add(new object[] { 3, "Java", "NetBeans IDE 7 Cookbook", "Rhawi Dantas" });
        dt.AcceptChanges();

        GridView1.DataSource = dt.DefaultView;
        GridView1.DataBind();

        //this line create a new DataView
        DataView dView = new DataView(dt);
        dView.Sort = "BookName DESC" ;

        Label1.Text = "Here we create a new DataView<br />" +
                      "and set the sort order (BookName DESC)";

        GridView2.DataSource = dView;
        GridView2.DataBind();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to use DataView Sort Property in ado.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:DarkBlue; font-style:italic;">
            How to use DataView Sort Property in ado.net
        </h2>
        <hr width="575" align="left" color="CornFlowerBlue" />
        <asp:GridView 
            ID="GridView1"
            runat="server"
            BorderColor="Snow"
            ForeColor="Snow"
            Width="575"
            Font-Names="Courier"
            >
            <HeaderStyle BackColor="OrangeRed" Height="30" />
            <RowStyle BackColor="DarkOrchid" />
            <AlternatingRowStyle BackColor="DarkMagenta" />
        </asp:GridView>
        <br />
        <asp:Label
             ID="Label1"
             runat="server"
             Font-Size="Large"
             ForeColor="DodgerBlue"
             Font-Italic="true"
             >
        </asp:Label>
        <br /><br />
        <asp:GridView 
            ID="GridView2"
            runat="server"
            BorderColor="Snow"
            ForeColor="Snow"
            Width="575"
            Font-Names="Courier"
            >
            <HeaderStyle BackColor="OrangeRed" Height="30" />
            <RowStyle BackColor="DarkOrchid" />
            <AlternatingRowStyle BackColor="DarkMagenta" />
        </asp:GridView>
        <br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Populate GridView"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>
More c# examples