Comprehensive Guide to Sorting DataTable: Correct Usage of DefaultView.Sort and Select

Dec 06, 2025 · Programming · 14 views · 7.8

Keywords: DataTable Sorting | DefaultView.Sort | Select Method

Abstract: This article delves into two core methods for sorting DataTable in .NET: DefaultView.Sort and Select. By analyzing common error cases, it explains why setting DefaultView.Sort does not alter the original order of DataTable and how to retrieve sorted data via DataView or iterating through DefaultView. The article compares the pros and cons of different approaches and provides complete code examples to help developers avoid common pitfalls and implement efficient data sorting.

Fundamentals of DataTable Sorting

In the .NET framework, DataTable is a widely used in-memory data storage structure, but its sorting mechanism often confuses developers. Many attempt to sort using table.DefaultView.Sort = "Town ASC, Customer ASC" or table.Select("", "Town ASC, Customer ASC"), only to find data displayed in its original order. This is not a code error but a misunderstanding of how DataTable sorting works.

Correct Usage of DefaultView.Sort

DefaultView is a DataView property of DataTable that provides sorting, filtering, and navigation capabilities. When setting the DefaultView.Sort property, you are modifying the sorting rules of the DataView, not the DataTable itself. Thus, accessing the DataTable's Rows collection still returns the original order. To retrieve sorted data, you must access it through DefaultView.

// Set sorting rules
table.DefaultView.Sort = "Town ASC, Customer ASC";
// Iterate through DefaultView to get sorted rows
foreach(DataRowView row in table.DefaultView)
{
    Console.WriteLine(row["Town"].ToString());
}

This method does not modify the original DataTable data but provides a sorted view, suitable for scenarios requiring temporary sorting without altering the data source.

Sorting Mechanism of the Select Method

The Select method of DataTable accepts filter and sort expressions as parameters and returns a DataRow array. Similar to DefaultView, it does not change the original order of DataTable but generates a new sorted array.

// Use Select method to get sorted row array
DataRow[] sortedRows = table.Select("", "Town ASC, Customer ASC");
foreach(DataRow row in sortedRows)
{
    Console.WriteLine(row["Town"].ToString());
}

The Select method is ideal for scenarios where you need to retrieve sorted results once and perform subsequent processing, but note it returns an array, not a DataTable.

Supplementary Sorting Methods

Beyond these methods, you can create a sorted DataTable via DataView. For example:

// Create DataView and set sorting
Dim dataView As New DataView(table)
dataView.Sort = "AutoID DESC, Name DESC"
// Convert sorted view to new DataTable
Dim sortedTable As DataTable = dataView.ToTable()

Or, a more concise approach:

table.DefaultView.Sort = "ColumnName ASC";
table = table.DefaultView.ToTable();

These methods generate a new DataTable with data sorted according to specified rules, suitable for scenarios requiring permanent changes to data order.

Summary and Best Practices

The key to understanding DataTable sorting lies in distinguishing between data source and view. Both DefaultView and Select provide sorted views without modifying original data. Use DefaultView iteration for displaying sorted data in UI or reports; use Select for sorted arrays for processing; and use DataView.ToTable for new sorted DataTables. Avoid relying directly on DataTable.Rows for sorted results to prevent common errors.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.