How to Manipulate Data in an ADO.NET* DataSet in a Mobilized Application

Submit New Article

December 9, 2008 11:00 PM PST



Challenge

Manipulate data in an ADO.NET DataSet in order to update it or present it to the viewer. Once an application has a DataSet with one or more DataTable objects, the application must be able to perform various operations on that data according to the needs of the application. Steps for creating the DataSet and populating it with data are covered in the separate item, How to Create an ADO.NET* DataSet for a Mobilized Application.


Solution

Implement a search of the data to retrieve a subset of the rows, and sort the data before displaying it to the user. After your application has a DataSet with one or more DataTable objects containing local data, you should implement a search of the data to retrieve a single row (or a set of rows) for display or retrieve a single row for local update. This can be achieved locally, without returning to the remote data source. The following table outlines the ways of searching a DataSet:

Object Method/Property Returns/Effect
DataTable Select method Array of DataRows
DataTable Contains method Boolean (True if match found)
DataTable.Rows Find method Single DataRow (first match)
DataView RowFilter property Filters the DataView
DataView Find method Integer index of row (first match)
DataView FindRows method Changes the collection of DataRowViews

 

Creating a DataView, or altering the default DataView, is one of the most useful and powerful of all the ways of searching, because you can bind user interface controls to a DataView; you cannot use data binding with an array of DataRows:

dsTitles.Tables[0].DefaultView.RowFilter = "royalty < 50";
dsTitles.Tables[0].DefaultView.RowFilter = "isnull(royalty,0) <=12";
dsTitles.Tables[0].DefaultView.RowFilter = "";
int iCount = dsTitles.Tables[0].DefaultView.Count;

 

Implement sorting when presenting the data within the DataSet to the user. As with filtering, there are several approaches:

Object Method/Property Returns/Comment
Data Adaptor Fill method Get the server to do the sorting for you by using an ORDER BY clause in the SQL
DataTable.Rows Add Sort the data when it is first created by appending the rows in the order that you want
DataTable Select method Array of DataRows
DataView Sort property Changes the collection of DataRowViews

 

You can set the Sort property of a DataView object to a string containing the column name(s) to be sorted, followed by an optional direction: ASC (for ascending—this is also the default direction if you do not give a direction) or DESC (for descending). You can specify multiple column names, for example:

dsTitles.Tables[0].DefaultView.Sort = "royalty ASC, price DESC";

 

This item may be most useful when used in conjunction with the following, separate items:

 


Source

Data Synchronization with the ADO.NET* DataSet