Introduction to DataTables: Part 2

 

In the first part of this two-parts article,we have constructed and loaded a datatable. Subsequently, we retrieveddata from it. In this part of the article, we will discuss andillustrate the following:

1.Different versions of a DataRow as it goesthrough its life cycle of being added, updated, and deleted in a givenapplication.

2.How to search a particular row in a DataTable using the primary key?

1. Different Versions of a DataRow:

There are a number of methods that are relatedto editing a DataRow in a DataTable. These are:DataTable.Rows[i].BeginEdit, DataTable.Rows[i].CancelEdit, andDataTable.Rows[i].EndEdit. Whereas, these methods operate on the i'throw of a DataTable, we may also apply the DataTable.AcceptChanges orDataTableReject.Changes methods to commit or reject all changes to anunderlying DataTable.

Before we go further, we would like to drawone's attention to two interesting issues here. Suppose that we aredisplaying the rows of our table in a bound DataGrid. When the userattempts to edit a data, the DataTable.Rows[i].BeginEdit would becalled automatically (unless we take other actions to disallowediting). Similarly, on the click event of an "Update Table" button, oron some other appropriate event, we may issue theDataTable.AcceptChanges. In this case the DataTable.DataRows[i].EndEditmethods for all changed rows will be invoked implicitly and the changeswill be committed. Accordingly, a DataTable.RejectChanges will invokethe CancelEdit methods of all uncommitted changed rows.

With these thoughts in mind, let us now getinto the "Version States" of a DataRowView. Although, we will not useany bound DataGrid in our example, we should know that "whenever datais displayed (for example in a DataGrid control), only one version ofeach row can be displayed. The displayed row is a DataRowView" (source:Microsoft .Net Documentation). There are four "version states" of adata row. These are: Default, Original, Current, and Proposed.

?Default: The default values of the columns, if any.
?Proposed: Whenever a BeginEdit is issued (explicitly or implicitly), the changed values become Proposed values.
?Current:These are the values after an EndEdit has been issued (explicitly orimplicitly). In this case, the proposed values will no longer exist.
?Original: These are the values prior to a BeginEdit. Subsequently, if a CancelEdit is issued, the proposed values are nullified.

Now we will present a simple example. In thisexample, we will perform addition, deletion and updates of some rows ofa datatable and display the appropriate versions.

We have started with a table named Productswith two columns. The column names are PCode and Price. The defaultvalues of these columns are assigned as "Some Default" and"9999999.99", respectively. The code for the example was designed toshow the version states of the datarows at the following stages:

?Stage 1. At first we added two rowsvia the DataTable.Rows.Add. At this stage, only the Default version andthe Current version exist. The values in both versions are identical.An attempt to display the Proposed or Original versions generates arun-time error.
?Stage 2. Subsequently, an AcceptChangesis issued. After this statement, we find that the Default, Original,and Current versions exist, and their values are identical. but theProposed version does not exist.
?Stage 3. Now the rowsare placed into "Edit" mode via the BeginEdit. The default value of thePCode of the first row is changed from "Some Default" to "P100" and thedefault Price of the second row is changed from 9999999.99 to 299.78.At this stage all of the four versions exist. The Default, Original andCurrent versions are identical. However, the Proposed version containsthe changed values.
?Stage 4. An EndEdit session is issuedfor each row. After the EndEdit is invoked, the Proposed version doesnot exist. Original version contains the old values, and the Currentversion contains the committed values.
?Stage 5. The second row is deleted. At this stage, the second row is marked for deletion, however, no version of the second row exist.
?Stage 6.An AcceptChanges is issued to the table. The second row is nowphysically deleted. Alternatively, instead of AcceptChanges, we couldhave also issued a RejectChanges to get the deleted row back.

Download DataRowView.cs

Compilation: We may compile the code as:

csc /r:System.dll,System.Data.dll DataRowView.cs

Output: Please compile and run the downloaded code to view the output.Discussion: While displaying the rows for various versions, wecould have tested if a particular version of a given row exist by usingthe Rows[i].HasVersion(DataRowVersion.VersionType). Please see the DotNet Framework documentation for detailed information.

At the first sight, the code and the issuesaddressed above may appear to be somewhat tedious and to some extent,irrelevant. However, bear in mind that all of these activities would beplaying major roles when you display a DataGrid on the client's screenin a web or windows application. In-depth knowledge of these issueswill allow you to provide more functionalities in the DataGrid ratherthan presenting a read-only view of the data. We refrain ourselves fromsuch discussions because the csharphelp.com would be publishing athree-part article on database connectivity authored by Selvaganapathi(http://www.csharphelp.com/archives/archive39.html).

2. How to Search a Particular Row in a DataTable Using the Primary Key?We may often need to find a particular row in a datatable. Of course,we can always resort to a sequential search. On the other hand, if theunderlying datatable has a primary key, we may employ the Find method,hopefully to find a record much faster. In the Dot Net documentation,the Find method is not very well explained and it is not clearlyspecified exactly what sort of algorithm does it employ to locate aparticular row. The Find method has a number of overloaded versions. Wewill discuss the one that matches a given value of the primary key andreturns the found row. Once the row is found, we can make changes tothe non-primary-key columns and update the row.

Example: In this example we will define adatatable named Departments. The DeptNumber column would be specifiedto be the primary key. We will load three rows in it. Subsequently wewill retrieve the row for department number 200, change its name andupdate the table.

Download DataTable4.cs

Compilation: csc /r:System.dll,System.Data.dll DataTable4.cs

Output:

Conclusion:

In this article, we have addressed the rowmanipulation techniques in a DataTable. Besides the objects and methodsillustrated in this article, the Do

t Net fr
amework has many otherclasses, collections and overloaded members that can be used to processDataTables. More articles in this area would be of great help.

References:
1. Microsoft Dot Net Framework Documentation
2. Harvey, et al. "C# Programming", Wrox Press, 2000
3. Selvaganapathi, K, "Accessing Data Using ADO.NET: Part 1 Of 3", Article in CsharpHelp.com
(http://www.csharphelp.com/archives/archive39.html), June 2001

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!