Anda di halaman 1dari 27

1/20/14

A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 2 - CodeProject


10,332,373 members (69,178 online) Sign in

home

articles

quick answers

discussions

features

community

help
Next

Search for articles, questions, tips

Articles Desktop Development Grid & Data Controls DataSets, DataGrids etc

Article Browse Code Stats Revisions Alternatives Comments & Discussions (12)

A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 2


By Pete2004, 23 Feb 2004
Rate this: 4.94 (42 votes)
Tw eet 1 Like 0 0 0

About Article
The purpose of this document is to provide a practical guide to using Microsofts .NET DataTables, DataSets and DataGrid Type Licence First Posted Views Bookmarked Article CPOL 23 Feb 2004 252,591 187 times

3 Tables
The architecture and capability of Tables should be understood since it carries over to understanding how aD a t a S e t and D a t a G r i d function. In the process of binding a DataGrid to a database the underlying code creates and associates collections of tables that are filled with data from the database. Also, a DataSet created from a database may contain tables with more information than needs to be displayed, columns may need to be added that are based upon complex formulas using information in other columns and data from multiple databases may need to be combined into a single tabular view. These operations are done by extracting information from these data sources and by filling a programmatically designed table that is unbound. Fundamentally a table contains C o l u m n s and R o w s collections, which means standard methods for accessing and manipulating collections can be used. The C o l u m n s collection contains, for each column, a name, a data type specification and maybe an assigned default value. Each table row in the Rows collection contains one cell for each column. The table class has an extensive set of methods for editing and managing versions of column and row data and for event notifications when changes occur. Figure 2 illustrates the overall architecture of a table.

.NET1.0 .NET1.1 C# Windows Visual-Studio , +

Top News
Programmers without TDD will be unemployable by 2022
Get the Insider News free each morning.

Related Videos

Related Articles
Matrix Multiplication in C# Creating animations with Dundas Chart for ASP.NET Smarter Data Labels with

www.codeproject.com/KB/grid/PracticalGuideDataGrids2.aspx

1/27

1/20/14

A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 2 - CodeProject


Dundas Chart SmartLabels Understanding Chart Areas with Dundas Chart for .NET A Formatted Text Box Using screensavers inside the Windows Media Player Making Sense of Geographic Data with Dundas Map and AJAX Handling connection notification between a desktop machine and Windows CE based devices Create data-driven applications with the Hera Application Framework Towards the self-documenting database: extended properties Accessibility audit vs. accessibility testing Digital Signatures and PDF Documents Color Scale Filter Merge Landscape and Portrait PDFs using ASP.NET Using Barcodes in Documents Best Practices How to Retrieve EMC Centera Cluster/Pool Capabilities Embedding IronPython in WPF Using C#

Figure 2 DataTable Decomposed

"Hey! Is That My Car? How to Sharpen a QuickBird Satellite Image Using DotImage" Integrate your SharePoint environment into the open standards-based WebSphere Portal platform using the Visual Studio IDE VBScript / Excel 2007 - An easy way to access DBF files Retrieving and Storing Call History

3.1 Table creation


A table memory object that will be able to contain/manage columns, rows and events can be easily created from the DataTable class as follows:
Collapse | Copy Code

/ /C r e a t eat a b l eo b j e c tb yu s i n gt h eD a t a T a b l ec l a s s : D a t a T a b l ed t=n e wD a t a T a b l e ( ) ; / /N a m et h et a b l eb ya s s i g n i n gad a t as t r i n gc o n t a i n i n g / /t h en a m et ot h et a b l e s / /T a b l e N a m ep r o p e r t y : d t . T a b l e N a m e= E l e m e n t s ; / /o ru s et h eD a t a T a b l e ( s t r i n gT a b l e N a m e )c o n s t r u c t o r D a t a T a b l ed t=n e wD a t a T a b l e ( E l e m e n t s ) ;

Related Research

3.2 Columns Creating and Adding to Tables


A table contains a collection of column definitions that will be used to define how each cell within a row can be referenced and the type of data content. The following scenario shows how to define a column and add it to a tables column collection. a. Define a Table as described in the Tables section. b. Create a column object to be added to the table by using the column class:
Collapse | Copy Code

Insider Secrets on API Security From Experts at Securosis [Webinar]

Ten Tips of Web App Testing

D a t a C o l u m nd c=n e wD a t a C o l u m n ( ) ; / /S e tt h ep r o p e r t i e sf o rt h ec o l u m n : / /s t r i n gn a m ef o rt h ec o l u m nt h a ti su s e da sa ni n d e x / /f o rc o l u m n sc o l l e c t i o n / /a n dac e l lw i t h i nar o w

www.codeproject.com/KB/grid/PracticalGuideDataGrids2.aspx

2/27

1/20/14

A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 2 - CodeProject


d c . C o l u m n N a m e= A t o m i c N b r ; / /s t r i n gn a m et h a ti su s e df o rac o l u m nl a b e lo rh e a d e r / /f o rd i s p l a yp u r p o s e s / /i fn o ts e t ,t h e nt h ed e f a u l tv a l u ei sd c . C o l u m n N a m e d c . C a p t i o n= A t o m i cN u m b e r ; / /o n eo ft h es t a n d a r ds y s t e md a t at y p e su s i n gt h e / /G e t T y p e ( )m e t h o d . d c . D a t a T y p e=S y s t e m . T y p e . G e t T y p e ( S y s t e m . I n t 3 2 ) ; / /o ro n ec o u l du s et h et y p e o fo p e r a t o r d c . D a t a T y p e=t y p e o f ( S y s t e m . I n t 3 2 ) ; / /ad e f a u l tv a l u et h a ti sa s s i g n e de a c ht i m e / /an e wr o wi sc r e a t e d d c . D e f a u l t V a l u e=0 ; / /o ru s eo n eo ft h eo t h e rc o n s t r u c t o r ss u c ha s / /D a t a C o l u m n ( s t r i n gC o l u m n N a m e ,S y s t e m . T y p eD a t a T y p e ) D a t a C o l u m nd c=n e wD a t a C o l u m n ( A t o m i c N b r , S y s t e m . T y p e . G e t T y p e ( S y s t e m . I n t 3 2 ) ) ;

c. Add the new column to the table C o l u m n scollection. The order in which the columns are added determines their zero-based index.
Collapse | Copy Code

d t . C o l u m n s . A d d ( d c ) ;

d. Repeat b and c for each column to be added to the table C o l u m n s collection.


Collapse | Copy Code

d c=n e wD a t a C o l u m n ( E l e m e n t ,S y s t e m . T y p e . G e t T y p e ( S y s t e m . S t r i n g ) ) ; d c . D e f a u l t V a l u e=s t r i n g . E m p t y ; d c . C a p t i o n= E l e m e n t ; d t . C o l u m n s . A d d ( d c ) ; d c=n e wD a t a C o l u m n ( S y m b o l ,S y s t e m . T y p e . G e t T y p e ( S y s t e m . S t r i n g )) ; d c . D e f a u l t V a l u e=s t r i n g . E m p t y ; d c . C a p t i o n= S y m b o l ; d t . C o l u m n s . A d d ( d c ) ; d c=n e wD a t a C o l u m n ( A t o m i c M a s s ,S y s t e m . T y p e . G e t T y p e ( S y s t e m . D e c i m a l )) ; d c . D e f a u l t V a l u e=0 . 0 ; d c . C a p t i o n= A t o m i cM a s s ; d t . C o l u m n s . A d d ( d c ) ;

Examples of data types supported in the .NET environment. Data Type .NET System Types

B o o l e a n B y t e B y t e [ ]( A r r a y ) C h a r( C h a r a r a c t e r ) D a t e T i m e D e c i m a l D o u b l e I n t e g e r S i n g l e S t r i n g U n s i g n e dI n t e g e r T i m e S p a n

S y s t e m . B o o l e a n S y s t e m . B y t e S y s t e m . B y t e [ ] S y s t e m . C h a r S y s t e m . D a t e T i m e S y s t e m . D e c i m a l S y s t e m . D o u b l e S y s t e m . I n t 1 6 ,S y s t e m . I n t 3 2 ,S y s t e m . I n t 6 4 S y s t e m . S i n g l e S y s t e m . S t r i n g S y s t e m . U I n t 1 6 ,S y s t e m . U I n t 3 2 ,S y s t e m . U I n t 6 4 S y s t e m . T i m e S p a n

At this point a table called Elements has been created with four columns AtomicNbr, Element, Symbol and AtomicMass with their respective data types and default values. The following three D i s p l a y C o l u m n I n f o ( )method code examples show this by using different techniques for accessing members and displaying data from their collections. In the first example, a for-loop is used to illustrate accessing table column collections through an integer index while in the second example a f o r e a c h loop illustrates accessing the same collections using a column class type. The third example uses strings containing the column names as an index. These accessing data examples illustrate the natural syntax approaches for working with collections. 1. f o r -loop
Collapse | Copy Code

p r i v a t ev o i dD i s p l a y C o l u m n I n f o ( D a t a T a b l ed t ) { l d e rC o l I n f o=n e wS t r i n g B u i l d e r ( ) ; C o l I n f o . A p p e n d F o r m a t ( C o l u m n \ t N a m e \ t D a t a T y p e \ n ) ; / /n o t et h a tt h et o t a ln u m b e ro fc o l u m n si nt h e

www.codeproject.com/KB/grid/PracticalGuideDataGrids2.aspx

3/27

1/20/14

A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 2 - CodeProject


/ /c o l l e c t i o ni sc o n t a i n e di nt h e C o u n t p r o p e r t y f o r ( i n tj = 0 ;j < d t . C o l u m n s . C o u n t ;j + + ) { C o l I n f o . A p p e n d F o r m a t ( [ { 0 } ] \ t { 1 } \ t { 2 } \t { 3 } \ n ,j , d t . C o l u m n s [ j ] . C o l u m n N a m e , d t . C o l u m n s [ j ] . C a p t i o n ,d t . C o l u m n s [ j ] . D a t a T y p e . T o S t r i n g ( ) ) ; } M e s s a g e B o x . S h o w ( C o l I n f o . T o S t r i n g ( ), C o l u m nN a m e , M e s s a g e B o x B u t t o n s . O K , M e s s a g e B o x I c o n . I n f o r m a t i o n ) ; }

2. f o r e a c hloop
Collapse | Copy Code

p r i v a t ev o i dD i s p l a y C o l u m n I n f o ( D a t a T a b l ed t ) { S t r i n g B u i l d e rC o l I n f o=n e wS t r i n g B u i l d e r ( ) ; C o l I n f o . A p p e n d F o r m a t ( C o l u m n \ t N a m e \ t D a t a T y p e \ n ) ; i n tj=1 ; f o r e a c h( D a t a C o l u m nd ci nd t . C o l u m n s ) { C o l I n f o . A p p e n d F o r m a t ( [ { 0 } ] \ t { 1 } \ t { 2 } \ t { 3 } \ n ,+ + j ,d c . C o l u m n N a m e , d c . C a p t i o n ,d c . D a t a T y p e . T o S t r i n g ( )) ; } M e s s a g e B o x . S h o w ( C o l I n f o . T o S t r i n g ( ) , C o l u m nN a m e ,M e s s a g e B o x B u t t o n s . O K , M e s s a g e B o x I c o n . I n f o r m a t i o n ) ;

3. Using known column names as indexes not column captions!


Collapse | Copy Code

p r i v a t ev o i dD i s p l a y C o l u m n D a t a T y p e I n f o ( D a t a T a }

3. Using known column names as indexes not column captions!


Collapse | Copy Code

p r i v a t ev o i dD i s p l a y C o l u m n D a t a T y p e I n f o ( D a t a T a b l ed t ) { S t r i n g B u i l d e rC o l I n f o=n e wS t r i n g B u i l d e r ( ) ; C o l I n f o . A p p e n d F o r m a t ( C o l u m n \ t N a m e \ t D a t a T y p e \ n ) ; C o l I n f o . A p p e n d F o r m a t ( [ { 0 } ] \ t { 1 } \ t { 2 } \ n , 1 , A t o m i c N b r , d t . C o l u m n s [ A t o m i c N b r ] . D a t a T y p e . T o S t r i n g ( ) ) ; C o l I n f o . A p p e n d F o r m a t ( [ { 0 } ] \ t { 1 } \ t { 2 } \ n , 1 , E l e m e n t , d t . C o l u m n s [ E l e m e n t ] . D a t a T y p e . T o S t r i n g ( ) ) ; C o l I n f o . A p p e n d F o r m a t ( [ { 0 } ] \ t { 1 } \ t { 2 } \ n , 1 , S y m b o l , d t . C o l u m n s [ S y m b o l ] . D a t a T y p e . T o S t r i n g ( ) ) ; C o l I n f o . A p p e n d F o r m a t ( [ { 0 } ] \ t { 1 } \ t { 2 } \ n , 1 , A t o m i c M a s s , d t . C o l u m n s [ A t o m i c M a s s ] . D a t a T y p e . T o S t r i n g ( ) ) ; M e s s a g e B o x . S h o w ( C o l I n f o . T o S t r i n g ( ), C o l u m nN a m e , M e s s a g e B o x B u t t o n s . O K , M e s s a g e B o x I c o n . I n f o r m a t i o n ) ;

3.3 Deleting/Removing Columns


Once a table has been defined columns can be deleted or removed as follows:
Collapse | Copy Code

/ /F o re x a m p l et od e l e t eac o l u m n A t o m i c M a s s d t . C o l u m n s . R e m o v e ( A t o m i c M a s s ) ; / /o ru s i n gaz e r o b a s e di n d e x A t o m i c M a s s i st h e / /4 t hc o l u m nw i t hi n d e x3 d t . C o l u m n s . R e m o v e A t ( 3 ) ; / /T om a k es u r et h a tac o l u m nc a nb er e m o v e d , / /f o re x a m p l e ,f i r s td e t e r m i n e / /w h e t h e rt h ec o l u m ne x i s t s ,b e l o n g st ot h et a b l e , / /o ri si n v o l v e di nac o n s t r a i n t / /o rr e l a t i o n . i f( d t . C o l u m n s . C o n t a i n s ( " A t o m i c M a s s " ) ) i f( d t . C o l u m n s . C a n R e m o v e ( d t . C o l u m n s [ " A t o m i c M a s s " ] ) {

www.codeproject.com/KB/grid/PracticalGuideDataGrids2.aspx

4/27

1/20/14

A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 2 - CodeProject


d t . C o l u m n s . R e m o v e ( A t o m i c M a s s ) ; }

3.4 Modifying Column Properties


Modifying a column property is simply accessing the property and setting its new value. For example:
Collapse | Copy Code

d t . C o l u m n s [ A t o m i c N b r ] . C o l u m n N a m e= A t o m i c N u m b e r ; d t . C o l u m n s [ A t o m i c M a s s ] . D a t a T y p e=t y p e o f ( S y s t e m . f l o a t ) ;

3.5 Clearing Column Collection


The entire table column collection can be cleared by simply using the C l e a r ( ) method.
Collapse | Copy Code

d t . C o l u m n s . C l e a r ( ) ;

3.6 Cloning a Table


Once a table has been defined it can be used to create an identical table with the same column collection or it can be used as a basis for a new table where columns will be deleted, added or modified. The original tables C l o n e ( ) method is used to create the new table with the same structure including schemas and constraints; however, it does not copy the content contained in the rows.
Collapse | Copy Code

D a t a T a b l ed t 1=d t . C l o n e ( ) ;

Now dt1 can be changed, for example:


Collapse | Copy Code

/ /d e l e t eac o l u m n d t 1 . C o l u m n s . R e m o v e ( A t o m i c M a s s ) ; / /a d dan e wc o l u m nt ot h et a b l e d c=n e wD a t a C o l u m n ( I s o t o p e N b r , S y s t e m . T y p e . G e t T y p e ( S y s t e m . I n t 3 2 ) ) ; d c . D e f a u l t V a l u e=0 ; d t 1 . C o l u m n s . A d d ( d c ) ; / /m o d i f yt h en a m ea n dc a p t i o no fa ne x i s t i n gc o l u m n d t 1 . C o l u m n s [ A t o m i c N b r ] . C o l u m n N a m e= A t o m i c N u m b e r ; d t 1 . C o l u m n s [ A t o m i c N b r ] . C a p t i o n= A t o m i cN u m b e r ;

3.7 Rows creating and adding to a table.


This section will show how to add rows and assign values to rows in the table rows collection using four equivalent methods for accessing individual cells within a row. The choice of method really depends upon the type of task such as the source of the data being used to fill the rows or simply extracting data from the rows. 1. Define a Table with Columns as described in the Tables and Columns section 2. The following scenario is the fundamental procedure for creating a row, filling the cells in the row and then adding the row to the table. This section also illustrates equivalent ways to index a cell, which provides the developer with much flexibility.
Collapse | Copy Code

/ /F i r s tc r e a t eaD a t a R o wv a r i a b l e D a t a R o wd r ; / /N e x tc r e a t ean e wr o wa n da s s i g ni tt ot h eD a t a R o w / /o b j e c td ru s i n gD a t a T a b l e s / /N e w R o w ( )m e t h o d . d r=d t . N e w R o w ( ) ; / /d rn o wc o n t a i n sac e l lf o re a c hc o l u m nd e f i n e di nt h e / /C o l u m n sc o l l e c t i o n

Four equivalent methods used to assign values to individual cells within a row.

www.codeproject.com/KB/grid/PracticalGuideDataGrids2.aspx

5/27

1/20/14

A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 2 - CodeProject

Method 1
Collapse | Copy Code

/ /f i l le a c hc e l lu s i n gaz e r o b a s e dc e l li n t e g e rc o l u m ni n d e x e s d r [ 0 ]=1 ; d r [ 1 ]= H y d r o g e n ; d r [ 2 ]= H ; d r [ 3 ]=1 . 0 0 7 8 ;

Method 2
Collapse | Copy Code

/ /f i l le a c hc e l lu s i n gt h ec o l u m nn a m ea st h es t r i n gc o l u m ni n d e x d r [ A t o m i c N b r ]=1 ; d r [ E l e m e n t ]= H y d r o g e n ; d r [ S y m b o l ]= H ; d r [ A t o m i c M a s s ]=1 . 0 0 7 8 ;

Method 3
Collapse | Copy Code

/ /f i l le a c hc e l lu s i n gD a t a C o l u m nd c-t h i si sm o r ea p p l i c a b l e / /w h e nu s i n gaD a t a C o l u m nf o r e a c hl o o p / /e . g .f o r e a c h( D a t a C o l u m nd ci nd t . R o w s ) D a t a C o l u m nd c ; d c=d t . C o l u m n s [ A t o m i c N b r ] ; d r [ d c ]=1 ; d c=d t . C o l u m n s [ E l e m e n t ] ; d r [ d c ]= H y d r o g e n ; d c=d t . C o l u m n s [ S y m b o l ] ; d r [ d c ]= H ; d c=d t . C o l u m n s [ A t o m i c M a s s ] ; d r [ d c ]=1 . 0 0 7 8 ;

Method 4
Collapse | Copy Code

/ /f i l le a c hc e l lu s i n gD a t a C o l u m nd ca n di t sC o l u m n N a m ep r o p e r t y / /w h i c hi si d e n t i c a lt oM e t h o d3b u ti s / /i n c l u d e dh e r ef o rc o m p l e t e n e s s / /A g a i n ,m o r ea p p l i c a b l ew h e nu s i n gaf o r e a c hl o o p D a t a C o l u m nd c ; d c=d t . C o l u m n s [ A t o m i c N b r ] ; d r [ d c . C o l u m n N a m e ]=1 ; d c=d t . C o l u m n s [ E l e m e n t ] ; d r [ d c . C o l u m n N a m e ]= H y d r o g e n ; d c=d t . C o l u m n s [ S y m b o l ] ; d r [ d c . C o l u m n N a m e ]= H ; d c=d t . C o l u m n s [ A t o m i c M a s s ] ; d r [ d c . C o l u m n N a m e ]=1 . 0 0 7 8 ;

/ /a d dt h er o wt ot h et a b l e sr o wc o l l e c t i o n d t . R o w s . A d d ( d r ) ;

3. This scenario can be easily extended to a more general procedure to add n rows to the table. For example suppose a two dimensional object array ElementData with n rows and dt.Columns.Count columns contains data to be added to the table. It could be loaded as follows:
Collapse | Copy Code

D a t a R o wd r ; i n tj ; f o r( i n ti = 0 ;i<n ;i + + ) { j=1 ; d r=d t . N e w R o w ( ) ; f o r e a c h( D a t a C o l u m nd ci nd t . C o l u m n s ) { j + + ; / /f i l le a c hc e l l ,u s i n gt h ec o l u m nd ca st h ei n d e x ,f r o mt h e / /p r e v i o u s l yd e f i n e dt w od i m e n s i o n a lo b j e c ta r r a yE l e m e n t D a t a i f( d c . D a t a T y p e= =t y p e o f ( S y s t e m . S t r i n g ) )

www.codeproject.com/KB/grid/PracticalGuideDataGrids2.aspx

6/27

1/20/14

A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 2 - CodeProject


d r [ d c ]=( S y s t e m . S t r i n g ) E l e m e n t D a t a [ i ] [ j ] ; e l s e i f( d c . D a t a T y p e= =t y p e o f ( S y s t e m . I n t 3 2 ) ) d r [ d c ]=( S y s t e m . I n t 3 2 ) E l e m e n t D a t a [ i ] [ j ] ; e l s e i f( d c . D a t a T y p e= =t y p e o f ( S y s t e m . D e c i m a l ) ) d r [ d c ]=( S y s t e m . D e c i m a l ) E l e m e n t D a t a [ i ] [ j ] ; } d t . R o w s . A d d ( d r ) ;

3.8 Modifying data within an existing table row


There are a number of ways to modify data in a table with the following illustrating the basic mechanism. Other techniques will be presented in the following sections.
Collapse | Copy Code

/ /F i r s tc r e a t eaD a t a R o wv a r i a b l e D a t a R o wd r ; / /N e x ta s s i g nt h er o wi nt h eR o w sc o l l e c t i o n / /t ob em o d i f i e dt od r ,f o re x a m p l es e l e c tr o w w i t hi n d e x=0 d r=d t . R o w s [ 0 ] ; / /S e l e c tt h e / /c o l u m nw i t h i nt h er o wt ob em o d i f i e db ys p e c i f y i n g / /ac o l u m ni n d e xa n da s s i g n t h en e wv a l u e d r [ A t o m i c N b r ]=1 . 0 0 7 8 1 ;

Equivalent alternative coding methods are as follows:


Collapse | Copy Code

d t . R o w s [ 0 ] [ A t o m i c N b r ]=1 . 0 0 7 8 1 ;

or
Collapse | Copy Code

d t . R o w s [ 0 ] [ 0 ]=1 . 0 0 7 8 1 ;

3.9 Fill Table using L o a d D a t a R o w ( ) method


The L o a d E l e m e n t D a t a R o w ( ) code example method in this section illustrates loading data into a table using the DataTables L o a d D a t a R o wmethod that takes an object containing data for each cell within a row. The L o a d D a t a R o w method is bracketed by B e g i n L o a d D a t a ( )and E n d L o a d D a t a ( )methods that turn off and on event notifications and other properties related to linked tables. Using these methods can prevent unnecessary processing by event handlers that would otherwise be triggered that are discussed in the Event Handler section. Also, the L o a d D a t a R o w method will modify an existing row if primary keys match or add the row to the Rows collection. Refer to the section on Row Versions that discusses the different versions of rows managed by the tables class for sample code illustrating the different behaviors of the L o a d D a t a R o wmethod when a table has a primary key and when it does not.
Collapse | Copy Code

p r i v a t eD a t a R o wL o a d E l e m e n t D a t a R o w ( D a t a T a b l ed t , i n tA t o m i c N b r ,s t r i n gE l e m e n t , s t r i n gS y m b o l ,d o u b l eA t o m i c M a s s ) { / /T u r n so f fe v e n tn o t i f i c a t i o n s , / /i n d e xm a i n t e n a n c e ,a n dc o n s t r a i n t s / /w h i l el o a d i n gd a t a d t . B e g i n L o a d D a t a ( ) ; / /A d dt h er o wv a l u e st ot h er o w sc o l l e c t i o na n d / /r e t u r nt h eD a t a R o w .I ft h es e c o n d / /a r g u m e n ti ss e tt ot r u e ,t h e nd t . A c c e p t C h a n g e s ( )i sc a l l e d / / o t h e r w i s en e wr o w sa r e / /m a r k e da sa d d i t i o n sa n dc h a n g e st oe x i s t i n gr o w sa r em a r k e da s / / m o d i f i c a t i o n s . D a t a R o wd r=d t . L o a d D a t a R o w ( n e wo b j e c t [ ] { A t o m i c N b r ,E l e m e n t ,S y m b o l ,A t o m i c M a s s } ,f a l s e ) ; / /T u r n so ne v e n tn o t i f i c a t i o n s ,i n d e xm a i n t e n a n c e ,a n dc o n s t r a i n t s / /t h a tw e r et u r n e do f f / /w i t ht h eB e g i n L o a d D a t a ( )m e t h o d d t . E n d L o a d D a t a ( ) ; r e t u r nd r ;/ /r e t u r n st h eD a t a R o wf i l l e d / /w i t ht h en e wv a l u e s }

www.codeproject.com/KB/grid/PracticalGuideDataGrids2.aspx

7/27

1/20/14

A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 2 - CodeProject

3.10Retrieving Table Content


The G e t T a b l e D a t a ( ) example method retrieves the column labels and row data from an input table and formats them into a string that can be used for printing, copying to the clipboard and exporting to a tab delimited text file.
Collapse | Copy Code

p r i v a t es t r i n gG e t T a b l e D a t a ( D a t a T a b l ed t ) { S t r i n g B u i l d e rT a b l e D a t a=n e wS t r i n g B u i l d e r ( ) ; / /r e t r i e v eh e a d e rr o wc o l u m nl a b e l s T a b l e D a t a . A p p e n d F o r m a t ( R o w ) ; f o r e a c h( D a t a C o l u m nd ci nd t . C o l u m n s ) T a b l e D a t a . A p p e n d F o r m a t ( \ t { 0 } ,d c . C o l u m n N a m e ) ; T a b l e D a t a . A p p e n d F o r m a t ( \ n ) ; / /r e t r i e v er o w s i n tj=1 ; f o r e a c h( D a t a R o wd ri nd t . R o w s ) { T a b l e D a t a . A p p e n d F o r m a t ( [ { 0 } ] , + + j ) ; f o r e a c h( D a t a C o l u m nd ci nd t . C o l u m n s ) { T a b l e D a t a . A p p e n d F o r m a t ( \ t { 0 } ,d r [ d c ]) ; } T a b l e D a t a . A p p e n d F o r m a t ( \ n ) ; } r e t u r nT a b l e D a t a . T o S t r i n g ( ) ; }

The output string for our element table with one row would look like the following when it is displayed in a grid format using an Excel spreadsheet or the DataGrid. Row [0] AtomicNbr 1 Element Hydrogen Symbol H AtomicMass 1.0078

3.11Row Versions and Accepting/Rejecting Changes


3.11.1 Methods and Enumerations
This is an important section to understand because the Table class maintains different states and versions of rows that can be used to provide rollback, undo and transaction logging capability. That is, this state and version information provides very powerful programmatic control over table data and UI strategies. Before discussing row states and versions there are Table and Row methods that need to be defined: Table Method Description Accepts all row changes to the table. Changes can be accepted to individual rows when the DataRow A c c e p t C h a n g e s ( ) method is called. Rejects all row changes to the table that have taken place since the last call to the Table or DataRow A c c e p t C h a n g e s ( ) . Returns a table containing all rows that have been modified. This is particular useful when building transaction logs to satisfy government and corporate regulations, such as CFR21-11. Note: If the Table A c c e p t C h a n g e sis called prior to

A c c e p t C h a n g e s ( )

R e j e c t C h a n g e s ( )

G e t C h a n g e s ( )

www.codeproject.com/KB/grid/PracticalGuideDataGrids2.aspx

8/27

1/20/14

A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 2 - CodeProject

G e t C h a n g e sthen there will be no changes and the return


value is null. Rows Method Description Adds a row to the rows collection Inserts a row at a specific position in the rows collection Removes a row at an index from the rows collection Accepts all changes to the row including changes to the individual cells including adding and deleting the row to and from the table respectively. Rejects changes to the row restoring the original values Begins a row editing session Cancels a row editing session and restores all previous values Ends a row editing session

A d d ( ) I n s e r t A t ( ) R e m o v e A t ( )

A c c e p t C h a n g e s ( )

R e j e c t C h a n g e s ( ) B e g i n E d i t ( )

C a n c e l E d i t ( )

E n d E d i t ( )

There are four different versions of Row Collections that are automatically maintained by the Tables object that provides extensive programmatic control over edits, deletes and inserts. DataRowVersion Description This version contains the current set of all values contained in each table row. The current set and the default set are identical during modifying a row without calling B e g i n E d i t ( ) after A c c e p t C h a n g e s ( )is called. The Default rows collection contains all of the changes. Each time a new row is created the new row is initialized to the default column values. Each time a cell value within a row is modified, the modification will be reflected in this table.

C u r r e n t

D e f a u l t

P r o p o s e d

This version as its name implies contains only rows that have proposed changes where they are only present during a call to B e g i n E d i t ( ) . When E n d E d i t ( )is called the proposed changes are reflected in the Current D a t a R o w , in the Original DataRow and proposed DataRow is deleted. When the C a n c e l E d i t is called, the proposed DataRow is deleted and the Default DataRow is changed back to the Current D a t a R o wvalues. The Original Rows collection is updated each time A c c e p t C h a n g e s ( ) is called. These values are used when R e j e c t C h a n g e s ( )and C a n c e l E d i t ( ) are called to return the values back to the state before any changes occurred since the last call to A c c e p t C h a n g e s ( ) . Description The row is marked as Added when a row is added or inserted to the Rows Collection and before an A c c e p t C h a n g e s ( ) method is called.

O r i g i n a l

RowState

A d d e d

www.codeproject.com/KB/grid/PracticalGuideDataGrids2.aspx

9/27

1/20/14

A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 2 - CodeProject

D e l e t e d

After A c c e p t C h a n g e s ( ) is called the row is marked as Deleted when any of the following is performed:

d r . D e l e t e ( ) d t . R o w s . R e m o v e A t ( i n d e x ) d t . R o w s . R e m o v e ( d r )
Before A c c e p t C h a n g e s ( ) is called the row is marked as Detached when any of the following is performed:

D e t a c h e d

d r . D e l e t e ( ) d t . R o w s . R e m o v e A t ( i n d e x ) d t . R o w s . R e m o v e ( d r )
After A c c e p t C h a n g e s ( ) is called any row cell value that is changed causes the row to be marked as Modified. After A c c e p t C h a n g e s ( ) is called the row or all rows are marked as Unchanged depending upon whether it is a DataRow A c c e p t C h a n g e s ( ) call or Table A c c e p t C h a n g e s ( ) call.
Collapse | Copy Code

M o d i f i e d

U n c h a n g e d

D a t a T a b l ed t=n e wD a t a T a b l e ( " E l e m e n t s " ) ; D a t a C o l u m nA t o m i c N b r=n e wD a t a C o l u m n ( " A t o m i c N b r " , S y s t e m . T y p e . G e t T y p e ( " S y s t e m . I n t 3 2 " ) ) ; A t o m i c N b r . D e f a u l t V a l u e = 0 ; d t . C o l u m n s . A d d ( A t o m i c N b r ) ; D a t a C o l u m nE l e m e n t=n e wD a t a C o l u m n ( " E l e m e n t " , S y s t e m . T y p e . G e t T y p e ( " S y s t e m . S t r i n g " ) ) ; E l e m e n t . D e f a u l t V a l u e =" E l e m e n t " ; d t . C o l u m n s . A d d ( E l e m e n t ) ; D a t a R o wd r ;

3.11.2 Sample 1 Row States


This section shows the different row states and the conditions for them.
Collapse | Copy Code

d r=d t . N e w R o w ( ) ; d r [ E l e m e n t ] = " H y d r o g e n " ; d r [ A t o m i c N b r ] =1 ; / /N e w R o wB e f o r eA d d :R o w S t a t e = D e t a c h e d d t . R o w s . A d d ( d r ) ; / /N e w R o wA f t e rA d d :R o w S t a t e = A d d e d d t . R o w s [ 0 ] . A c c e p t C h a n g e s ( ) ; / / N e w R o wA f t e rA c c e p t C h a n g e s :R o w S t a t e = U n c h a n g e d d t . R o w s . R e m o v e A t ( 0 ) ; / /n o t et h a tt h er o wi sm a r k e da sD e t a c h e dw h e n / /R e m o v e A t ( )o rR e m o v e ( )i su s e d / / N e w R o wA f t e rR e m o v e A t :R o w S t a t e = D e t a c h e d / /A d dt h er o wb a c k ,a c c e p tt h ec h a n g e sa n dt h e nd e l e t et h er o w / /n o t et h a tt h er o ws t a t ei sn o wm a r k e da sd e l e t e dw h e nD e l e t e ( ) / /i fD e l e t e ( )i sc a l l e dp r i o rt ot h er o wb e i n g / /a d d e dt h e nt h er o wi sm a r k e da sD e t a c h e d . d t . R o w s . A d d ( d r ) ; d t . A c c e p t C h a n g e s ( ) ; d r . D e l e t e ( ) ; / /N e w R o wA f t e rD e l e t e :< R o w S t a t e = D e l e t e d >

The following code examples will illustrate the above method functionality. After each section of code will be four tables, one each for each type of DataRowVersion, that will show whether the version contains a row and if so their respective values. NOTE Rows that do not exist (designated with a No value under column Has Versions) for a particular RowState version are added or included for readability and clarity. That is, if all the rows in a version table were listed, these would not be in the Table Rows collection. The presentation schema is from code Sample 2 to Sample N where each successive code Sample uses the results from the previous Sample. All changes that occur to the version tables for Sample code section are designated in bold red.

www.codeproject.com/KB/grid/PracticalGuideDataGrids2.aspx

10/27

1/20/14

A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 2 - CodeProject

3.11.3 Sample 2 Initial Loading of Table


Collapse | Copy Code

d r=d t . N e w R o w ( ) ; d r [ E l e m e n t ] = " H y d r o g e n " ; d r [ A t o m i c N b r ] =1 ; d t . R o w s . A d d ( d r ) ; d r=d t . N e w R o w ( ) ; d r [ E l e m e n t ] = " H e l i u m " ; d r [ A t o m i c N b r ] =2 ; d t . R o w s . A d d ( d r ) ; d r=d t . N e w R o w ( ) ; d r [ E l e m e n t ] = " L i t h i u m " ; d r [ A t o m i c N b r ] =3 ; d t . R o w s . A d d ( d r ) ; d r=d t . N e w R o w ( ) ; / /t h i sr o wc o n t a i n sd e f a u l tv a l u e s d t . R o w s . A d d ( d r ) ;

Row 0 has only an Original Version and it is marked as Deleted. The Current and Default versions are identical with the four new rows being marked as Added. The Proposed version table does not contain any values.

Current Version
Row [0] [1] [2] [3] [4] Has Versions No Yes Yes Yes Yes Added Added Added Added 1 2 3 0 Hydrogen Helium Lithium Element Row State AtomicNbr Element

Default Version
Row [0] [1] [2] [3] Has Versions No Yes Yes Yes Added Added Added 1 2 3 Hydrogen Helium Lithium Row State AtomicNbr Element

[4]

Yes

Added

Element

Original Version
Row [0] [1] [2] Has Versions Yes No No Row State Deleted AtomicNbr 0 Element Element

www.codeproject.com/KB/grid/PracticalGuideDataGrids2.aspx

11/27

1/20/14
[3] [4]

A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 2 - CodeProject


No No

Proposed Version
Row [0] [1] [2] [3] [4] Has Versions No No No No No Row State AtomicNbr Element

3.11.4 Sample 3 DataRow AcceptChanges


Collapse | Copy Code

d t . R o w s [ 0 ] . A c c e p t C h a n g e s ( ) ; d t . R o w s [ 1 ] . A c c e p t C h a n g e s ( ) ;

After the R o w s [ 0 ] . A c c e p t C h a n g e s ( )was called, the row that was marked deleted, row 0 in the above version tables, has been deleted from all versions and all other rows have new indices. The next A c c e p t C h a n g e s ( ) command references row 1 in the newly ordered Rows Collection. In this case, the Row State is marked as Unchanged in versions Current, Default and Original. In the Original version there is only one row and it corresponds to Row 1 that was accepted. The Proposed version table does not contain any values. Current Version Row [0] [1] [2] [3] Default Version Row [0] [1] [2] [3] Original Version Has Versions Yes Yes Yes Yes Row State Added Unchanged Added Added AtomicNbr 1 2 3 0 Element Hydrogen Helium Lithium Element Has Versions Yes Yes Yes Yes Row State Added Unchanged Added Added AtomicNbr 1 2 3 0 Element Hydrogen Helium Lithium Element

www.codeproject.com/KB/grid/PracticalGuideDataGrids2.aspx

12/27

1/20/14
Row

A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 2 - CodeProject


Has Versions Row State AtomicNbr Element

[0] [1] [2] [3] Proposed Version Row [0] [1] [2] [3]

No Yes No No Unchanged 2 Helium

Has Versions No No No No

Row State

AtomicNbr

Element

3.11.5 Sample 4 Table AcceptChanges


Collapse | Copy Code

d t . A c c e p t C h a n g e s ( ) ;

After the T a b l e . A c c e p t C h a n g e s ( ) is called, all remaining rows are marked Unchanged in the Current, Default and Original Version tables and the Original version table is identical to the Current and Default version tables. The Proposed version table does not contain any rows. Current Version Row [0] [1] [2] [3] Default Version Row [0] [1] [2] [3] Original Version Has Versions Yes Yes Yes Yes Row State Unchanged Unchanged Unchanged Unchanged AtomicNbr 1 2 3 0 Element Hydrogen Helium Lithium Element Has Versions Yes Yes Yes Yes Row State Unchanged Unchanged Unchanged Unchanged AtomicNbr 1 2 3 0 Element Hydrogen Helium Lithium Element

www.codeproject.com/KB/grid/PracticalGuideDataGrids2.aspx

13/27

1/20/14
Row

A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 2 - CodeProject


Has Versions Row State AtomicNbr Element

[0] [1] [2] [3] Proposed Version Row [0] [1] [2] [3]

Yes Yes Yes Yes

Unchanged Unchanged Unchanged Unchanged

1 2 3 0

Hydrogen Helium Lithium Element

Has Versions No No No No

Row State

AtomicNbr

Element

3.11.6 Sample 5 DataRow BeginEdit


Collapse | Copy Code

d t . R o w s [ 1 ] . B e g i n E d i t ( ) ; d t . R o w s [ 1 ] [ " E l e m e n t " ] =" H e l i u m " ; d t . R o w s [ 1 ] [ " A t o m i c N b r " ] =2 2 2 ;

The above code begins an editing session on row 1 and the new values are reflected in the Default version table and values now appear in the Proposed version table. All other table version entries remain unchanged. Current Version Row [0] [1] [2] [3] Default Version Row [0] [1] [2] [3] Has Versions Yes Yes Yes Yes Row State Unchanged Unchanged Unchanged Unchanged AtomicNbr 1 222 3 0 Element Hydrogen Helium Lithium Element Has Versions Yes Yes Yes Yes Row State Unchanged Unchanged Unchanged Unchanged AtomicNbr 1 2 3 0 Element Hydrogen Helium Lithium Element

www.codeproject.com/KB/grid/PracticalGuideDataGrids2.aspx

14/27

1/20/14
Original Version Row [0] [1] [2] [3] Proposed Version Row [0] [1] [2] [3]

A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 2 - CodeProject

Has Versions Yes Yes Yes Yes

Row State Unchanged Unchanged Unchanged Unchanged

AtomicNbr 1 2 3 0

Element Hydrogen Helium Lithium Element

Has Versions No Yes No No

Row State

AtomicNbr

Element

Unchanged

222

Helium

3.11.7 Sample 6 DataRow CancelEdit


Collapse | Copy Code

d t . R o w s [ 1 ] . C a n c e l E d i t ( ) ;

The C a n c e l E d i t ( ) command returns the default values back to the Original state and clears out the Proposed values from row 1 in the Proposed version table. Current Version Row [0] [1] [2] [3] Default Version Row [0] [1] [2] [3] Original Version Has Versions Yes Yes Yes Yes Row State Unchanged Unchanged Unchanged Unchanged AtomicNbr 1 2 3 0 Element Hydrogen Helium Lithium Element Has Versions Yes Yes Yes Yes Row State Unchanged Unchanged Unchanged Unchanged AtomicNbr 1 2 3 0 Element Hydrogen Helium Lithium Element

www.codeproject.com/KB/grid/PracticalGuideDataGrids2.aspx

15/27

1/20/14
Row [0] [1] [2] [3] Proposed Version Row [0] [1] [2] [3]

A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 2 - CodeProject


Has Versions Yes Yes Yes Yes Row State Unchanged Unchanged Unchanged Unchanged AtomicNbr 1 2 3 0 Element Hydrogen Helium Lithium Element

Has Versions No No No No

Row State

AtomicNbr

Element

3.11.8 Sample 7 DataRow BeginEdit Example 2


Collapse | Copy Code

d t . R o w s [ 3 ] . B e g i n E d i t ( ) ;

The B e g i n E d i t ( ) method initializes the Proposed row 3 with default values. Current Version Row [0] [1] [2] Has Versions Yes Yes Yes Row State Unchanged Unchanged Unchanged AtomicNbr 1 2 3 Element Hydrogen Helium Lithium

[3] Default Version Row [0] [1] [2] [3] Original Version

Yes

Unchanged

Element

Has Versions Yes Yes Yes Yes

Row State Unchanged Unchanged Unchanged Unchanged

AtomicNbr 1 2 3 0

Element Hydrogen Helium Lithium Element

www.codeproject.com/KB/grid/PracticalGuideDataGrids2.aspx

16/27

1/20/14
Row [0] [1] [2] [3] Proposed Version Row [0] [1] [2] [3]

A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 2 - CodeProject


Has Versions Yes Yes Yes Yes Row State Unchanged Unchanged Unchanged Unchanged AtomicNbr 1 2 3 0 Element Hydrogen Helium Lithium Element

Has Versions No No No Yes

Row State

AtomicNbr

Element

Unchanged

Element

3.11.9 Sample 8 DataRow Change values Example 2


Collapse | Copy Code

d t . R o w s [ 3 ] [ " E l e m e n t " ] = " C a r b o n " ; d t . R o w s [ 3 ] [ " A t o m i c N b r " ] =1 2 ;

The Default and Proposed row 3 values have been changed to reflect Carbon and 12. All other rows in all versions remain unchanged. Current Version Row [0] [1] [2] [3] Default Version Row [0] [1] [2] [3] Original Version Has Versions Yes Yes Yes Yes Row State Unchanged Unchanged Unchanged Unchanged AtomicNbr 1 2 3 12 Element Hydrogen Helium Lithium Carbon Has Versions Yes Yes Yes Yes Row State Unchanged Unchanged Unchanged Unchanged AtomicNbr 1 2 3 0 Element Hydrogen Helium Lithium Element

www.codeproject.com/KB/grid/PracticalGuideDataGrids2.aspx

17/27

1/20/14
Row [0] [1]

A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 2 - CodeProject


Has Versions Yes Yes Row State Unchanged Unchanged AtomicNbr 1 2 Element Hydrogen Helium

[2] [3] Proposed Version Row [0] [1] [2] [3]

Yes Yes

Unchanged Unchanged

3 0

Lithium Element

Has Versions No No No Yes

Row State

AtomicNbr

Element

Unchanged

12

Carbon

3.11.10 Sample 9 DataRow EndEdit Modified Rows


Collapse | Copy Code

d t . R o w s [ 3 ] . E n d E d i t ( ) ; d t . R o w s [ 0 ] [ " E l e m e n t " ]=" O x y g e n " ; d t . R o w s [ 0 ] [ " A t o m i c N b r " ]=8 ; / /A d dan e wr o w . d r=d t . N e w R o w ( ) ; d t . R o w s . A d d ( d r ) ;

After E n d E d i t ( ) is called, the Current and Default versions for row 3 are updated and marked as Modified. The Original version for row 3 still retains the original values before the changes and is marked as Modified. The next two lines assign Oxygen and its atomic number to row 0 and these values are reflected in the Current and Default versions, which are also marked as Modified. The Original version for row 0 remains unchanged. The next two lines adds a new row to the Current and Default version tables initialized with default values and marked as Added. Note that the new row does not appear in the Original version table. Current Version Row [0] Has Versions Yes Row State Modified AtomicNbr 8 Element Oxygen

[1] [2] [3] [4] Default Version

Yes Yes Yes Yes

Unchanged Unchanged Modified Added

2 3 12 0

Helium Lithium Carbon Element

www.codeproject.com/KB/grid/PracticalGuideDataGrids2.aspx

18/27

1/20/14
Row [0] [1] [2] [3] [4] Original Version Row [0] [1] [2] [3] [4] Proposed Version Row [0] [1] [2] [3] [4]

A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 2 - CodeProject


Has Versions Yes Yes Yes Yes Yes Row State Modified Unchanged Unchanged Modified Added AtomicNbr 8 2 3 12 0 Element Oxygen Helium Lithium Carbon Element

Has Versions Yes Yes Yes Yes No

Row State Modified Unchanged Unchanged Modified

AtomicNbr 1 2 3 0

Element Hydrogen Helium Lithium Element

Has Versions No No No No No

Row State

AtomicNbr

Element

3.11.11 Sample 10 DataRow AcceptChanges of Modified Rows


Collapse | Copy Code

d t . R o w s [ 3 ] . A c c e p t C h a n g e s ( ) ;

The DataRow A c c e p t C h a n g e s ( ) for row 3 causes the corresponding row in the Current and Default version tables to be marked as Unchanged and the Original version table now contains the same row values. Current Version Row [0] [1] Has Versions Yes Yes Row State Modified Unchanged AtomicNbr 8 2 Element Oxygen Helium

www.codeproject.com/KB/grid/PracticalGuideDataGrids2.aspx

19/27

1/20/14
[2] [3] [4] Default Version Row [0] [1] [2] [3] [4] Original Version Row [0] [1] [2] [3] [4] Proposed Version Row [0] [1] [2] [3] [4]

A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 2 - CodeProject


Yes Yes Yes Unchanged Unchanged Added 3 12 0 Lithium Carbon Element

Has Versions Yes Yes Yes Yes Yes

Row State Modified Unchanged Unchanged Unchanged Added

AtomicNbr 8 2 3 12 0

Element Oxygen Helium Lithium Carbon Element

Has Versions Yes Yes Yes Yes No

Row State Modified Unchanged Unchanged Unchanged

AtomicNbr 1 2 3 12

Element Hydrogen Helium Lithium Carbon

Has Versions No No No No No

Row State

AtomicNbr

Element

3.11.12 Sample 11 DataRow RejectChanges


Collapse | Copy Code

d t . R o w s [ 0 ] . R e j e c t C h a n g e s ( ) ;

Calling the DataRow R e j e c t C h a n g e s ( ) method for row 0 causes the corresponding row values for the Current and Default tables to revert to the Original version values and three tables, Current, Default and Original, have the Row State for row 0 marked as unchanged.

www.codeproject.com/KB/grid/PracticalGuideDataGrids2.aspx

20/27

1/20/14
Current Version Row [0] [1] [2] [3] [4] Default Version Row [0] [1] [2] [3] [4] Original Version Row [0] [1] [2] [3] [4] Proposed Version Row [0] [1] [2] [3] [4]

A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 2 - CodeProject

Has Versions Yes Yes Yes Yes Yes

Row State Unchanged Unchanged Unchanged Unchanged Added

AtomicNbr 1 2 3 12 0

Element Hydrogen Helium Lithium Carbon Element

Has Versions Yes Yes Yes Yes Yes

Row State Unchanged Unchanged Unchanged Unchanged Added

AtomicNbr 1 2 3 12 0

Element Hydrogen Helium Lithium Carbon Element

Has Versions Yes Yes Yes Yes No

Row State Unchanged Unchanged Unchanged Unchanged

AtomicNbr 1 2 3 12

Element Hydrogen Helium Lithium Carbon

Has Versions No No No No No

Row State

AtomicNbr

Element

www.codeproject.com/KB/grid/PracticalGuideDataGrids2.aspx

21/27

1/20/14

A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 2 - CodeProject

3.11.13 Sample 12 LoadDataRow without table having primary key


Collapse | Copy Code

d t . B e g i n L o a d D a t a ( ) ; d t . L o a d D a t a R o w ( n e wo b j e c t [ ] { 1 , " D e u t e r i u m " } ,f a l s e ) ; d t . E n d L o a d D a t a ( ) ;

If a table does not have a primary key then the LoadDataRow method will create a new row and fill it with values in the object array. Looking at rows 0 and 5 in the Current and Default version tables, they both have the same atomic number, but different element names. Also, Row 5 is marked as being Added. Current Version Row [0] [1] [2] [3] [4] [5] Default Version Row [0] [1] [2] [3] [4] [5] Original Version Row [0] [1] [2] [3] [4] [5] Has Versions Yes Yes Yes Yes No No Row State Unchanged Unchanged Unchanged Unchanged AtomicNbr 1 2 3 12 Element Hydrogen Helium Lithium Carbon Has Versions Yes Yes Yes Yes Yes Yes Row State Unchanged Unchanged Unchanged Unchanged Added Added AtomicNbr 1 2 3 12 0 1 Element Hydrogen Helium Lithium Carbon Element Deuterium Has Versions Yes Yes Yes Yes Yes Yes Row State Unchanged Unchanged Unchanged Unchanged Added Added AtomicNbr 1 2 3 12 0 1 Element Hydrogen Helium Lithium Carbon Element Deuterium

www.codeproject.com/KB/grid/PracticalGuideDataGrids2.aspx

22/27

1/20/14
Proposed Version Row [0] [1] [2] [3] [4] [5]

A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 2 - CodeProject

Has Versions No No No No No No

Row State

AtomicNbr

Element

3.11.14 Sample 13 LoadDataRow with table having primary key


Collapse | Copy Code

/ /m u s td e l e t et h er o ww i t had u p l i c a t e / /A t o m i c N b ri no r d e rt oc r e a t e / /ap r i m a r yk e y . d t . R o w s . R e m o v e A t ( 5 ) ; d t . A c c e p t C h a n g e s ( ) ; / /C r e a t eap r i m a r yk e ya n dl o a dt h en e wo b j e c ta r r a yd a t a . d t . P r i m a r y K e y=n e wD a t a C o l u m n [ ]{ d t . C o l u m n s [ " A t o m i c N b r " ] } ; d t . B e g i n L o a d D a t a ( ) ; d t . L o a d D a t a R o w ( n e wo b j e c t [ ] { 1 , " D e u t e r i u m " } ,f a l s e ) ; d t . E n d L o a d D a t a ( ) ;

If a table has a primary key then the L o a d D a t a R o wmethod will modify the data in the row if the primary keys match or else it will append the row to the table. Looking at row 0 in the Current and Default version tables, the element name has been changed from Hydrogen to Deuterium. In the three version tables Current, Default and Original row 0 is now marked as being Modified. Current Version Row [0] [1] [2] Has Versions Yes Yes Yes Row State Modified Unchanged Unchanged AtomicNbr 1 2 3 Element Deuterium Helium Lithium

[3] [4] Default Version Row [0] [1] [2]

Yes Yes

Unchanged Unchanged

12 0

Carbon Element

Has Versions Yes Yes Yes

Row State Modified Unchanged Unchanged

AtomicNbr 1 2 3

Element Deuterium Helium Lithium

www.codeproject.com/KB/grid/PracticalGuideDataGrids2.aspx

23/27

1/20/14
[3] [4] Original Version Row [0] [1] [2] [3] [4] Proposed Version Row [0] [1] [2] [3] [4]

A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 2 - CodeProject


Yes Yes Unchanged Unchanged 12 0 Carbon Element

Has Versions Yes Yes Yes Yes Yes

Row State Modified Unchanged Unchanged Unchanged Unchanged

AtomicNbr 1 2 3 12 0

Element Hydrogen Helium Lithium Carbon Element

Has Versions No No No No No

Row State

AtomicNbr

Element

3.11.15 Sample Code for Obtaining Version and State Information


The above tables were generated using the following procedure.
Collapse | Copy Code

s t a t i cv o i dP r i n t R o w V e r s i o n s ( D a t a T a b l ed t ) { D a t a R o w V e r s i o n [ ]r o w V e r=n e wD a t a R o w V e r s i o n [ 4 ] ; r o w V e r [ 0 ]=D a t a R o w V e r s i o n . C u r r e n t ; r o w V e r [ 1 ]=D a t a R o w V e r s i o n . D e f a u l t ; r o w V e r [ 2 ]=D a t a R o w V e r s i o n . O r i g i n a l ; r o w V e r [ 3 ]=D a t a R o w V e r s i o n . P r o p o s e d ; S t r i n g B u i l d e rT a b l e D a t a=n e wS t r i n g B u i l d e r ( ) ; f o r ( i n ti = 0 ;i < r o w V e r . L e n g t h ;i + + ) { / /P r i n tt h ev a l u eo fe a c hc o l u m ni ne a c hr o w . T a b l e D a t a . A p p e n d F o r m a t ( " { 0 }V e r s i o n \ n " ,r o w V e r [ i ] . T o S t r i n g ( ) ) ; / /r e t r i e v eh e a d e rr o wc o l u m nl a b e l s T a b l e D a t a . A p p e n d F o r m a t ( " R o w \ t H a sV e r s i o n s \ t R o wS t a t e " ) ; f o r e a c h( D a t a C o l u m nd ci nd t . C o l u m n s ) T a b l e D a t a . A p p e n d F o r m a t ( " \ t { 0 } " ,d c . C o l u m n N a m e ) ; T a b l e D a t a . A p p e n d F o r m a t ( " \ n " ) ; i n tn = 1 ; f o r e a c h ( D a t a R o wr o wi nd t . R o w s) { n + + ; i f( r o w . H a s V e r s i o n ( r o w V e r [ i ] )) { / /P r i n tt h es p e c i f i e dv e r s i o no ft h er o w ' sv a l u e . T a b l e D a t a . A p p e n d F o r m a t ( " [ { 0 } ] \ t Y e s \ t { 1 } " , n . T o S t r i n g ( ) ,r o w . R o w S t a t e . T o S t r i n g ( ) ) ; f o r e a c h( D a t a C o l u m nd ci nd t . C o l u m n s ) { T a b l e D a t a . A p p e n d F o r m a t ( " \ t { 0 } " ,r o w [ d c , r o w V e r [ i ] ] ) ; } T a b l e D a t a . A p p e n d F o r m a t ( " \ n " ) ; }

www.codeproject.com/KB/grid/PracticalGuideDataGrids2.aspx

24/27

1/20/14

A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 2 - CodeProject


e l s e { T a b l e D a t a . A p p e n d F o r m a t ( " [ { 0 } ] \ t N o \ t " ,n . T o S t r i n g ( ) ) ; f o r ( i n tj = 0 ;j < d t . C o l u m n s . C o u n t ;j + + ) T a b l e D a t a . A p p e n d F o r m a t ( " \ t" ) ; T a b l e D a t a . A p p e n d F o r m a t ( " \ n " ) ; }

} T a b l e D a t a . A p p e n d F o r m a t ( " \ n " ) ; } / /o u t p u ts t r i n gd a t at oat e x tf i l eu s i n gaS t r e a m W r i t e r / /S t r e a m W r i t e rs w=n e wS t r e a m W r i t e r ( " c : \ R o w V e r s i o n s . t x t " ) ; s w . W r i t e ( T a b l e D a t a . T o S t r i n g ( ) ) ; / /s w . C l o s e ( ) ;

3.12Handling DataTable Errors


A DataTable can be checked to determine if it contains any rows with errors by examining the Tables H a s E r r o r s property value. The following code illustrates how to isolate the rows and their columns with errors.
Collapse | Copy Code

i f( d t . H a s E r r o r s ) {/ /E r r o r sh a v eo c c u r r e di nr o w si nt a b l ed t f o r e a c h( D a t a R o wd ri nd t . R o w s ) { i f ( d r . H a s E r r o r s ) { / /R o wh a se r r o r s / /G e t C o l u m n s I n E r r o r ( )r e t u r n sa na r r a yo f / /D a t a C o l u m n st h a tc o n t a i ne r r o r s f o r e a c h ( D a t a C o l u m nd ci nd r . G e t C o l u m n s I n E r r o r ( ) ) { / /G e t C o l u m n E r r o rr e t u r n sad e s c r i p t i o no ft h eC o l u m ne r r o r M e s s a g e B o x . S h o w ( d r . G e t C o l u m n E r r o r ( d c . O r d i n a l ) ) ; } } } }

3.13DataTable Events
The following code provides an example of adding a DataTable column changed event handler and the code within the handler illustrates some techniques for processing the new column values.
Collapse | Copy Code

d t . C o l u m n C h a n g e d+ =n e wD a t a C o l u m n C h a n g e E v e n t H a n d l e r ( t h i s . S a m p l e F o r m _ C o l u m n C h a n g e d ) ; p r i v a t ev o i dS a m p l e F o r m _ C o l u m n C h a n g e d ( o b j e c ts e n d e r , S y s t e m . D a t a . D a t a C o l u m n C h a n g e E v e n t A r g se ) { i f ( e . C o l u m n . O r d i n a l< o p > ) { / /c o u l dp e r f o r mv a l i d a t i o nc h e c k ss u c ha sr a n g ev a l u e s / /o rf o r m a t t i n go ro t h e rt y p e so f / / p r o c e s s i n go nt h ec h a n g e dc o l u m n . } i f ( e . R o w . H a s E r r o r s ) { / /c l e a rt h ee r r o r e . R o w . S e t C o l u m n E r r o r ( e . C o l u m n ,s t r i n g . E m p t y ) ; / /c h e c kt os e ei fr o wh a sa n ym o r ee r r o r s D a t a C o l u m n[ ]d c E r r o r s=e . R o w . G e t C o l u m n s I n E r r o r ( ) ; / /i ft h e r ea r en om o r ee r r o r st h e nc l e a rt h er o we r r o rf l a g . i f ( d c E r r o r s . L e n g t h= =0 ) e . R o w . C l e a r E r r o r s ( ) ; } }

Next...
DataSets

License
www.codeproject.com/KB/grid/PracticalGuideDataGrids2.aspx 25/27

1/20/14

A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 2 - CodeProject


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author


Pete2004
President Bioxing United States

Ph.D. in BioPhysics and over 20 years of experience in managing, architecting and hands-on developing software systems for biotechnology companies that produced cutting edge instrumentation and data systems. These include DNA and Peptide Synthesizers, cDNA, oligo and Protein microarrays and mass spectrometers used for protein sequencing.In 2001 founded BioXing (pronounced Bio-Crossing) which has architected and developed an extensible relational database repository and software system that includes Web Services, Client Workstation and Web Based applications. The system is used to track, manage, integrate and data mine disparate laboratory data, protocols and experiments and link to reference proteomic and genomic data. BioXing also does consulting and development for biotechnology companies. Article Top

Comments and Discussions


You must Sign In to use this message board. Search this forum Profile popups Spacing Relaxed Noise Very High Layout Normal Per page 50 Go Update

First Prev Next

Message Automatically Removed My vote of 5 Genius piece of Work how to merge a newly created or modified row back to a typed dataset? Backcolor for separate rows DataRow in Datatable in C# Some error here. Re: Some error here. download as single document Re: download as single document Adding,Deleting,Editing data in a dataGrid part 2 missing

Lalit24rocks Raja.D.Singh HarinderS dotNetKeen

15-May-13 19:47 8-Nov-11 20:12 24-Aug-11 17:59 9-Jun-08 6:11

michailk2001

3-Jul-06 16:47

Robert Shih Pete2004 mooncow Pete2004 Rameshi Jonathan de Halleux

23-Feb-05 15:15 23-Feb-05 17:54 7-Feb-05 21:34 8-Feb-05 4:51 13-Dec-04 22:38 24-Feb-04 20:24

www.codeproject.com/KB/grid/PracticalGuideDataGrids2.aspx

26/27

1/20/14

A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 2 - CodeProject


Re: part 2 missing
Last Visit: 31-Dec-99 18:00 General News Last Update: 19-Jan-14 21:50 Suggestion Question Bug Answer Joke Rant netclectic 24-Feb-04 23:36 Refresh Admin 1

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
Permalink | Advertise | Privacy | Mobile Web03 | 2.8.140119.1 | Last Updated 24 Feb 2004 Layout: fixed | fluid Article Copyright 2004 by Pete2004 Everything else Copyright CodeProject, 1999-2014 Terms of Use

www.codeproject.com/KB/grid/PracticalGuideDataGrids2.aspx

27/27

Anda mungkin juga menyukai