C# DataView Usage

Data: conceptual illustration

DataView selectively filters and sorts DataTable rows. The DataView class in System.Data is ideal for sorting data for UIs or for database INSERT. We look at ways you can utilize the data in your DataTable by sorting it with DataView.

These C# examples use the DataView type from System.Data. DataView filters and sorts.

Methods

Note

First, the DataTable you are using stores the physical data, while the DataView is only a view of that data. This means that you cannot easily sort a DataTable without using a DataView. Fortunately, DataView offers a convenient Sort string, which you can specify the column to sort with.

DataTable Where you populate your data, from the user or database.

DataView Accessed with the DefaultView property on DataTable. DataViews allow you to filter and sort data, not store it. Use DataView for inputting filtered data to your database. It is useful for displaying on a window or web page.

DefaultView Access this property on your DataTable instance. This is an instance of DataView.

Count This is an instance property on all DataView instances. You can use this in a for loop on the DataView.

Sort This is a string property on every DataView. Assign this to a string containing the name of a column. After you specify this, you can loop over the DataView.

Example

Here we see an example of how you can use DataView to sort one of four columns on a DataTable. In your program, the DataTable may be generated from user input or a database, but here we create it programmatically. The GetTable method returns a table with four columns.

Program that uses DataView [C#]

using System;
using System.Data;

class Program
{
    static void Main()
    {
	//
	// Specify the column to sort on.
	//
	DataTable table = GetTable();
	table.DefaultView.Sort = "Weight";

	//
	// Display all records in the view.
	//
	DataView view = table.DefaultView;
	Console.WriteLine("=== Sorted by weight ===");
	for (int i = 0; i < view.Count; i++)
	{
	    Console.WriteLine("{0}, {1}, {2}, {3}",
		view[i][0],
		view[i][1],
		view[i][2],
		view[i][3]);
	}

	//
	// Now sort on the Name.
	//
	view.Sort = "Name";
	//
	// Display all records in the view.
	//
	Console.WriteLine("=== Sorted by name ===");
	for (int i = 0; i < view.Count; i++)
	{
	    Console.WriteLine("{0}, {1}, {2}, {3}",
		view[i][0],
		view[i][1],
		view[i][2],
		view[i][3]);
	}
    }

    /// <summary>
    /// This example method generates a DataTable.
    /// </summary>
    static DataTable GetTable()
    {
	//
	// Here we create a DataTable with four columns.
	//
	DataTable table = new DataTable();
	table.Columns.Add("Weight", typeof(int));
	table.Columns.Add("Name", typeof(string));
	table.Columns.Add("Breed", typeof(string));
	table.Columns.Add("Date", typeof(DateTime));

	//
	// Here we add unsorted data to the DataTable and return.
	//
	table.Rows.Add(57, "Koko", "Shar Pei", DateTime.Now);
	table.Rows.Add(130, "Fido", "Bullmastiff", DateTime.Now);
	table.Rows.Add(92, "Alex", "Anatolian Shepherd Dog", DateTime.Now);
	table.Rows.Add(25, "Charles", "Cavalier King Charles Spaniel", DateTime.Now);
	table.Rows.Add(7, "Candy", "Yorkshire Terrier", DateTime.Now);
	return table;
    }
}

Output

=== Sorted by weight ===
7, Candy, Yorkshire Terrier, 4/5/2009 3:38:22 PM
25, Charles, Cavalier King Charles Spaniel, 4/5/2009 3:38:22 PM
57, Koko, Shar Pei, 4/5/2009 3:38:22 PM
92, Alex, Anatolian Shepherd Dog, 4/5/2009 3:38:22 PM
130, Fido, Bullmastiff, 4/5/2009 3:38:22 PM

=== Sorted by name ===
92, Alex, Anatolian Shepherd Dog, 4/5/2009 3:38:22 PM
7, Candy, Yorkshire Terrier, 4/5/2009 3:38:22 PM
25, Charles, Cavalier King Charles Spaniel, 4/5/2009 3:38:22 PM
130, Fido, Bullmastiff, 4/5/2009 3:38:22 PM
57, Koko, Shar Pei, 4/5/2009 3:38:22 PM
Main method

Overview. The program first has the Main entry point, which we use to sort the DataTable. The final method, GetTable, is a custom method that returns a new DataTable. Before we can use DataView, we must have a complete DataTable.

DataTable. The GetTable method returns a DataTable with four columns, each specify an attribute of a dog. The four columns are: Weight, Name, Breed, and Date. You can find more information about DataTables.

DataTable Examples

DataView. In the Main method, the DefaultView property on the DataTable is accessed. DefaultView is a property of type DataView. This means it has a Sort property. The code assigns the Sort property to a string that matches one of the column names. In the example, the column used in "Weight".

Finishing the code. The first for loop displays all fields in the DataTable, sorted by the Weight integer. Then, the DataView is resorted on Name, and then that view is displayed.

Output. In the first section, the Weight integer is ordered in ascending order; in the second section, the Name string is ordered in ascending ASCII sort order.

Programming tip

Filter

You can also filter your DataView, which allows you to exclude rows from your DataView. This means you can view your DataTable with certain rows matching a condition omitted. The two properties to use here are RowFilter, and RowStateFilter.

Summary

The C# programming language

Here we saw a complete example of using DataView to sort rows in a DataTable. This is powerful because it allows you to sort your data before using it in your Windows Forms or ASP.NET program, and before INSERT-ing it into your SQL database. We reviewed the concepts behind DataView and their relation to DataTable.

Data
.NET