Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
home
articles
quick answers
discussions
features
community
help
Next
Articles Desktop Development Grid & Data Controls DataSets, DataGrids etc
Article Browse Code Stats Revisions Alternatives Comments & Discussions (12)
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.
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
"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
Related Research
www.codeproject.com/KB/grid/PracticalGuideDataGrids2.aspx
2/27
1/20/14
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 ) ;
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
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 ) ;
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 }
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 ) ;
www.codeproject.com/KB/grid/PracticalGuideDataGrids2.aspx
4/27
1/20/14
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 ) ;
d t . C o l u m n s . C l e a r ( ) ;
D a t a T a b l ed t 1=d t . C l o n e ( ) ;
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
Method 1
Collapse | Copy Code
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
Method 4
Collapse | Copy Code
/ /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
www.codeproject.com/KB/grid/PracticalGuideDataGrids2.aspx
6/27
1/20/14
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 ;
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
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
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 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
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 ;
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
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]
Proposed Version
Row [0] [1] [2] [3] [4] Has Versions No No No No No Row State AtomicNbr Element
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
[0] [1] [2] [3] Proposed Version Row [0] [1] [2] [3]
Has Versions No No No No
Row State
AtomicNbr
Element
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
[0] [1] [2] [3] Proposed Version Row [0] [1] [2] [3]
1 2 3 0
Has Versions No No No No
Row State
AtomicNbr
Element
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]
AtomicNbr 1 2 3 0
Row State
AtomicNbr
Element
Unchanged
222
Helium
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]
Has Versions No No No No
Row State
AtomicNbr
Element
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
AtomicNbr 1 2 3 0
www.codeproject.com/KB/grid/PracticalGuideDataGrids2.aspx
16/27
1/20/14
Row [0] [1] [2] [3] Proposed Version Row [0] [1] [2] [3]
Row State
AtomicNbr
Element
Unchanged
Element
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]
Yes Yes
Unchanged Unchanged
3 0
Lithium Element
Row State
AtomicNbr
Element
Unchanged
12
Carbon
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
2 3 12 0
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]
AtomicNbr 1 2 3 0
Has Versions No No No No No
Row State
AtomicNbr
Element
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]
AtomicNbr 8 2 3 12 0
AtomicNbr 1 2 3 12
Has Versions No No No No No
Row State
AtomicNbr
Element
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]
AtomicNbr 1 2 3 12 0
AtomicNbr 1 2 3 12 0
AtomicNbr 1 2 3 12
Has Versions No No No No No
Row State
AtomicNbr
Element
www.codeproject.com/KB/grid/PracticalGuideDataGrids2.aspx
21/27
1/20/14
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]
Has Versions No No No No No No
Row State
AtomicNbr
Element
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
Yes Yes
Unchanged Unchanged
12 0
Carbon Element
AtomicNbr 1 2 3
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]
AtomicNbr 1 2 3 12 0
Has Versions No No No No No
Row State
AtomicNbr
Element
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
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
Next...
DataSets
License
www.codeproject.com/KB/grid/PracticalGuideDataGrids2.aspx 25/27
1/20/14
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
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
michailk2001
3-Jul-06 16:47
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
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