Sort DataTable in C#

  1. Sort DataTable With the DataView.Sort Property in C
  2. Sort DataTable With the DataTable.DefaultView Property in C

This tutorial will introduce the methods to sort a datatable in C#.

Sort DataTable With the DataView.Sort Property in C

The DataView.Sort property is used to get or set the sort column of a datatable in C#. We can set the sort column of our datatable by specifying the column name like DataView.Sort = "Col_name". By default, this method sorts the datatable in ascending order. We can specify desc after the column name to sort the datatable in descending order. We can then convert this DataView to a DataTable with the DataView.ToTable() function in C#. The following code example shows us how we can sort a datatable with the DataView.Sort property in C#.

using System;
using System.Data;

namespace datatable_sort
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable table1 = new DataTable();
            DataColumn column1 = new DataColumn();
            DataColumn column2 = new DataColumn();
            column1.DataType = System.Type.GetType("System.Decimal");
            column2.DataType = System.Type.GetType("System.Decimal");
            column1.ColumnName = "Price";
            column2.ColumnName = "Rating";
            table1.Columns.Add(column1);
            table1.Columns.Add(column2);
            DataRow row;
            for (int i = 0; i < 3; i++)
            {
                row = table1.NewRow();
                row["Price"] = i + 1;
                row["Rating"] = i;
                table1.Rows.Add(row);
            }
            //Displaying Original Values
            Console.WriteLine("UnSorted Values");
            foreach(DataRow r in table1.Rows)
            {
                Console.WriteLine("Price = {0}, Rating = {1}",r[0],r[1]);
            }

            //Sorting the Table
            DataView dv = table1.DefaultView;
            dv.Sort = "Price desc";
            DataTable sortedtable1 = dv.ToTable();
            
            //Displaying Sorted Values
            Console.WriteLine("Sorted Values by Descending order of Price");
            foreach (DataRow r in sortedtable1.Rows)
            {
                Console.WriteLine("Price = {0}, Rating = {1}", r[0], r[1]);
            }

        }
    }
}

Output:

UnSorted Values
Price = 1, Rating = 0
Price = 2, Rating = 1
Price = 3, Rating = 2
Sorted Values by Descending order of Price
Price = 3, Rating = 2
Price = 2, Rating = 1
Price = 1, Rating = 0

In the above code, we first created a datatable table1 and added two columns to it Price and Rating. Then we sorted the table by the descending order of Price and displayed the new values. We created a new instance of the DataView class and sorted it with the DataView.Sort property. We then converted the sorted DataView to a DataTable with the DataView.ToTable() function in C#. In the end, we displayed the data in the sorted table.

Sort DataTable With the DataTable.DefaultView Property in C

The DataTable.DefaultView property is used to get a customized view of a datatable in C#. We can sort our datatable by specifying the sort column in DataTable.DefaultView.Sort property. By default, this method sorts the datatable in ascending order. We can specify desc after the column name to sort the datatable in descending order. We can then convert that sorted view to a datatable with the DefaultView.ToTable() function in C#. The following code example shows us how we can sort a datatable with the DataTable.DefaultView property in C#.

using System;
using System.Data;

namespace datatable_sort
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable table1 = new DataTable();
            DataColumn column1 = new DataColumn();
            DataColumn column2 = new DataColumn();
            column1.DataType = System.Type.GetType("System.Decimal");
            column2.DataType = System.Type.GetType("System.Decimal");
            column1.ColumnName = "Price";
            column2.ColumnName = "Rating";
            table1.Columns.Add(column1);
            table1.Columns.Add(column2);
            DataRow row;
            for (int i = 0; i < 3; i++)
            {
                row = table1.NewRow();
                row["Price"] = i + 1;
                row["Rating"] = i;
                table1.Rows.Add(row);
            }
            //Displaying Original Values
            Console.WriteLine("UnSorted Values");
            foreach (DataRow r in table1.Rows)
            {
                Console.WriteLine("Price = {0}, Rating = {1}", r[0], r[1]);
            }
            
            //Sorting the Table
            table1.DefaultView.Sort = "Price desc";
            table1 = table1.DefaultView.ToTable(true);
            
            //Displaying Sorted Values
            Console.WriteLine("Sorted Values by Descending order of Price");
            foreach (DataRow r in table1.Rows)
            {
                Console.WriteLine("Price = {0}, Rating = {1}", r[0], r[1]);
            }

        }
    }
}

Output:

UnSorted Values
Price = 1, Rating = 0
Price = 2, Rating = 1
Price = 3, Rating = 2
Sorted Values by Descending order of Price
Price = 3, Rating = 2
Price = 2, Rating = 1
Price = 1, Rating = 0

In the above code, we first created a datatable table1 and added two columns, Price and Rating. Then we sorted the table table1 by the descending order of Price and displayed the new values. We created a sorted view of our table1 table with table1.DefaultView.Sort property. We then converted that sorted view to a table with the table1.DefaultView.ToTable(true) function in C#. In the end, we displayed the data in the sorted table.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Csharp DataTable

  • Create DataTable in C#