a practical guide to .net datatables, datasets and datagrids - part 3 dataset
TRANSCRIPT
-
8/13/2019 A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 3 Dataset
1/9
1/20/14 A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 3 - CodeProject
www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D 1/9
10,332,373 members (70,435 on line) Sign in
home quick answers discussions featur es comm unity help Search for articles, questions, tips
Articles Desktop Development Grid & Data Controls DataSets, DataGrids etc
Article
Browse Code
Stats
Revisions
Alternatives
Comments &Discussions (39)
About Article
The purpose of thisdocument is to provide apractical guide to usingMicrosofts .NETDataTables, DataSets andDataGrid
Type Article
Licence
First Posted 23 Feb 2004
Views 251,356
Bookmarked 142 times
.NET1.0 .NET1.1 C#
Windows Visual-Studio ,
+
Next
Rate this:
0TweetTweet 0
A Practical Guide to .NET D ataTabl es, DataSets andDataGrids - Part 3By Pete2004 , 23 Feb 2004
4 Data Sets
4.1 DataSet Methods
DataSet Method Description
AcceptChanges() Accepts all changes to the DataSet
Clea r() Removes all rows from all tables in the DataSet that is,removes all data.
Clone()Creates a new DataSet with all tab les having the same Tablestructure including any constraints and relationships. NoData is copied.
Copy() Same as for the DataSet Clone() but it includes all Data.
GetChanges()
Creates a DataSet that contains all changes made to thedataset. If AcceptChanges was called then only changes
made since the last call are returned.
HasChanges() Returns true if any changes were made to the DataSetincluding adding tables and modifying rows.
WriteXml() Outputs an XML file containing schema with all Tables,Data, Constraints and relationships.
ReadXml() Inputs an XML file containing schema, Tables, Data,Constraints and relationships..
4.2 DataSet Properties
DataSet Property Description
4.59 (28 votes)00LikeLike
articles
http://adclick.g.doubleclick.net/aclk?sa=L&ai=BNoqx4xvdUr-gGau5igfu4IGoAgAAAAAQASAAOABQqryNUViG_a0aYOWCgIC8DoIBCWNhLWdvb2dsZbIBGHd3dy5kY2xrLWRlZmF1bHQtcmVmLmNvbcgBAtoBIGh0dHA6Ly93d3cuZGNsay1kZWZhdWx0LXJlZi5jb20vqAMB4AQCmgUZCJGCVBCFobgyGI3i54QBIIb9rRooybfkAdoFAggBoAYU4AbJt-QB&num=0&sig=AOD64_2TZAzfHmK2OAU_rNQk9he22vIaCg&client=&adurl=http://www.newrelic.comhttp://adclick.g.doubleclick.net/aclk?sa=L&ai=BNoqx4xvdUr-gGau5igfu4IGoAgAAAAAQASAAOABQqryNUViG_a0aYOWCgIC8DoIBCWNhLWdvb2dsZbIBGHd3dy5kY2xrLWRlZmF1bHQtcmVmLmNvbcgBAtoBIGh0dHA6Ly93d3cuZGNsay1kZWZhdWx0LXJlZi5jb20vqAMB4AQCmgUZCJGCVBCFobgyGI3i54QBIIb9rRooybfkAdoFAggBoAYU4AbJt-QB&num=0&sig=AOD64_2TZAzfHmK2OAU_rNQk9he22vIaCg&client=&adurl=http://www.newrelic.comhttp://www.codeproject.com/http://www.learnnowonline.com/express?utm_source=cp&utm_medium=web&utm_campaign=WR1081Xhttp://www.learnnowonline.com/express?utm_source=cp&utm_medium=web&utm_campaign=WR1081Xhttps://www.codeproject.com/script/Membership/LogOn.aspx?rp=%2fArticles%2f6180%2fA-Practical-Guide-to-NET-DataTables-DataSets-and-Dhttp://www.codeproject.com/script/Articles/Latest.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/script/Membership/View.aspx?mid=801806http://twitter.com/search?q=http%3A%2F%2Fwww.codeproject.com%2FArticles%2F6180%2FA-Practical-Guide-to-NET-DataTables-DataSets-and-Dhttps://twitter.com/intent/tweet?hashtags=CodeProject&original_referer=http%3A%2F%2Fwww.codeproject.com%2FArticles%2F6180%2FA-Practical-Guide-to-NET-DataTables-DataSets-and-D&text=A%20Practical%20Guide%20to%20.NET%20DataTables%2C%20DataSets%20and%20DataGrids%20-%20Part%203%20-%20CodeProject&tw_p=tweetbutton&url=http%3A%2F%2Fwww.codeproject.com%2FArticles%2F6180%2FA-Practical-Guide-to-NET-DataTables-DataSets-and-Dhttp://www.codeproject.com/script/Articles/PrevNextLookup.aspx?aid=6180&at=1&secId=3http://www.codeproject.com/script/common/TellFriend.aspx?obtid=2&obid=6180http://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D?display=Printhttp://www.codeproject.com/Tags/Visual-Studiohttp://www.codeproject.com/Tags/Windowshttp://www.codeproject.com/Tags/C--hash--http://www.codeproject.com/Tags/.NET1.1http://www.codeproject.com/Tags/.NET1.0http://www.codeproject.com/script/Articles/Types.aspx?#Articlehttp://www.codeproject.com/MasterPages/#_commentshttp://www.codeproject.com/script/Articles/ListAlternatives.aspx?aid=6180http://www.codeproject.com/script/Articles/ListVersions.aspx?aid=6180http://www.codeproject.com/script/Articles/Statistics.aspx?aid=6180http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=6180http://adclick.g.doubleclick.net/aclk?sa=L&ai=BNoqx4xvdUr-gGau5igfu4IGoAgAAAAAQASAAOABQqryNUViG_a0aYOWCgIC8DoIBCWNhLWdvb2dsZbIBGHd3dy5kY2xrLWRlZmF1bHQtcmVmLmNvbcgBAtoBIGh0dHA6Ly93d3cuZGNsay1kZWZhdWx0LXJlZi5jb20vqAMB4AQCmgUZCJGCVBCFobgyGI3i54QBIIb9rRooybfkAdoFAggBoAYU4AbJt-QB&num=0&sig=AOD64_2TZAzfHmK2OAU_rNQk9he22vIaCg&client=&adurl=http://www.newrelic.comhttp://www.codeproject.com/KB/grid/#DataSets%2c+DataGrids+etchttp://www.codeproject.com/KB/grid/http://www.codeproject.com/Chapters/1/Desktop-Development.aspxhttp://www.codeproject.com/script/Content/SiteMap.aspxhttp://www.codeproject.com/KB/FAQs/http://www.codeproject.com/Lounge.aspxhttp://www.codeproject.com/Feature/http://www.codeproject.com/script/Forums/List.aspxhttp://www.codeproject.com/script/Answers/List.aspx?tab=activehttp://www.codeproject.com/http://www.learnnowonline.com/express?utm_source=cp&utm_medium=web&utm_campaign=WR1081Xhttps://www.codeproject.com/script/Membership/LogOn.aspx?rp=%2fArticles%2f6180%2fA-Practical-Guide-to-NET-DataTables-DataSets-and-Dhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#Mainhttp://www.codeproject.com/http://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#Main -
8/13/2019 A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 3 Dataset
2/9
1/20/14 A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 3 - CodeProject
www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D 2/9
Top News
Programmers without TDDwill be unemployable by2022
Get the Insider News free eachmorning.
Related Videos
Related Articles
Matrix Multiplication in C#
Creating animations withDundas Chart for ASP.NET
Smarter Data Labels withDundas Chart SmartLabels
Understanding Chart Areas withDundas Chart for .NET
A Formatted Text Box
Using screensavers inside theWindows Media Player
Making Sense of GeographicData with Dundas Map and
AJAXHandling connectionnotification between a desktopmachine and Windows CEbased devices
Create data-driven applicationswith the Hera ApplicationFramework
Towards the self-documentingdatabase: extended properties
Accessibility audit vs.accessibility testing
Digital Signatures and PDFDocuments
Color Scale Filter
Merge Landscape and PortraitPDFs using ASP.NET
Using Barcodes in Documents Best Practices
How to Retrieve EMC CenteraCluster/Pool Capabilities
Embedding IronPython in WPFUsing C#
"Hey! Is That My Car? How toSharpen a QuickBird SatelliteImage Using DotImage"
Integrate your SharePointenvironment into the openstandards-based WebSpherePortal platform using the VisualStudio IDE
CaseSensitive If set to true then string compares in DataSet tables arecase sensitive otherwise they are not.
DataSetName Name of the DataSet
HasErrors Returns true if there are any errors within any tables in theDataSet
Relationss
Collection of Relations
Method/PropertyDescription
Add()
AddRange()
If two or more relations are to beadded to the collection they can beadded using this method. They areappended to the existing collection inthe order specified in the range.
CanRemove Returns true if the relation can be
removed from the collectionn
Clear() Removes all relations from therelations collection
Contains() Returns a true if the collection containsthe named relation.
Count Returns the number of relations in thecollection
IndexOf() Returns the index of a relation in the
collection equal to name.
Remove() Removes a relation by Name from thecollection
RemoveAt() Removes a relation by index from thecollection.
Tables
Collection of Tables
Method/PropertyDescription
Add() Adds a table to the collection
AddRange()
If two or more tables are to be addedto the collection they can be addedusing this method. They are appendedto the existing collection in the orderspecified in the range.
Clear() Removes all tables from the collection
Contains() Returns a true if the collection containsa table with TableName equal to name.
http://www.codeproject.com/Articles/22127/Integrate-your-SharePoint-environment-into-the-opehttp://www.codeproject.com/Articles/21286/Hey-Is-That-My-Car-How-to-Sharpen-a-QuickBird-Satehttp://www.codeproject.com/Articles/21119/Embedding-IronPython-in-WPF-Using-Csharphttp://www.codeproject.com/Articles/20943/How-to-Retrieve-EMC-Centera-Cluster-Pool-Capabilithttp://www.codeproject.com/Articles/20940/Using-Barcodes-in-Documents-Best-Practiceshttp://www.codeproject.com/Articles/20499/Merge-Landscape-and-Portrait-PDFs-using-ASP-NEThttp://www.codeproject.com/Articles/19266/Color-Scale-Filterhttp://www.codeproject.com/Articles/18599/Digital-Signatures-and-PDF-Documentshttp://www.codeproject.com/Articles/17141/Accessibility-audit-vs-accessibility-testinghttp://www.codeproject.com/Articles/17077/Towards-the-self-documenting-database-extended-prohttp://www.codeproject.com/Articles/16856/Create-data-driven-applications-with-the-Hera-Applhttp://www.codeproject.com/Articles/16208/Handling-connection-notification-between-a-desktophttp://www.codeproject.com/Articles/15952/Making-Sense-of-Geographic-Data-with-Dundas-Map-anhttp://www.codeproject.com/Articles/14368/Using-screensavers-inside-the-Windows-Media-Playerhttp://www.codeproject.com/Articles/12417/A-Formatted-Text-Boxhttp://www.codeproject.com/Articles/8099/Understanding-Chart-Areas-with-Dundas-Chart-for-NEhttp://www.codeproject.com/Articles/7184/Smarter-Data-Labels-with-Dundas-Chart-SmartLabelshttp://www.codeproject.com/Articles/6930/Creating-animations-with-Dundas-Chart-for-ASP-NEThttp://www.codeproject.com/Tips/262862/Matrix-Multiplication-in-Csharp.aspxhttp://codeproject.tv/video/6951729/codeproject_interviews_anders_hejlsberg_and_steve_lucco_microsoft_technical_fellowshttp://codeproject.tv/video/6953598/building_your_first_app_with_xamarin_ioshttp://www.codeproject.com/Feature/Insider/http://css.dzone.com/articles/programmers-without-tdd-will -
8/13/2019 A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 3 Dataset
3/9
1/20/14 A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 3 - CodeProject
www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D 3/9
VBScript / Excel 2007 - An easyway to access DBF files
Retrieving and Storing CallHistory
Related Research
The Essential Guide to MobileApp Testing: Tips for
Developers in USA & Canada
Protecting Your Business Data:
Five Dos and Donts
Count Returns the number of tables in thecollection
IndexOf()Returns the index of a table in thecollection with a TableName equal toname.
Remove()Removes a table by TableName fromthe collection
RemoveAt() Removes a table by index from thecollection.
4.3 Loading A DataSet
4.3.1 From a Table
Tables created and filled with data as discussed in the Tables section can be added to the Tables collectionby using the Add() method or they can be added at once using the AddRange() method.
Example of Two equivalent methods used to add tables dtElements and dtIsotopes to the DataSet
ElementDS using Add() and AddRange()
Method 1 Tables.Add()
Collapse | Copy Code
// Add the El ements table to the DataS etelementDS.Tables.Add(dtElements);
// Add the Is otopes table to the DataS etelementDS.Tables.Add(dtIsotopes);
Method 2 Tables.AddRange()
Collapse | Copy Code
ElementDS.Tables.AddRange( new DataTable(){dtElements, dtIsotopes});
4.3.2 From a Database
A DataSet tables collection can also be filled with linked tables containing data directly from a databaserecordset, which is considered bound data.
4.3.2.1 Method 1 sqlDataAdapter
The following code illustrates how to directly load or bind a database recordset using the sqlDataAdaptersFill() method where the select query string was used to create the recordset. After the Fill() method iscalled, ds will contain a table in its collection with column headers that match the field names in the selectquery string and column datatypes will match those specified in the database table elements. Each row willcontain data corresponding to each field.
Collapse | Copy Code
System.Data.SqlClient.SqlConnection sqlConnection1;System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;System.Data.SqlClient.SqlCommand sqlSelectCommand1;sqlConnection1 = new System.Data.SqlClient.SqlConnection();sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();sqlSelectCommand1.CommandText = "SELECT ElementsID, AtomicNbr,+ Symbol, AtomicMass, Element FROM [Elements]" ;sqlDataAdapter1.SelectCommand = sqlSelectCommand1;
// // sql Conne ction1 // //
sqlConnection1.ConnectionString = workstation id=xxxxx;packet + size=4096;user id=nnnnn; pwd=yyyyy;data + source=xxxxx; persist+ security info=False; initial catalog=dbName;sqlSelectCommand1.Connection = sqlConnection1;
http://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/ResearchLibrary/40/Protecting-Your-Business-Data-Five-Do-s-and-Don-tshttp://www.codeproject.com/ResearchLibrary/19/The-Essential-Guide-to-Mobile-App-Testing-Tips-forhttp://www.codeproject.com/Articles/22262/Retrieving-and-Storing-Call-Historyhttp://www.codeproject.com/Articles/22187/VBScript-Excel-2007-An-easy-way-to-access-DBF-file -
8/13/2019 A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 3 Dataset
4/9
1/20/14 A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 3 - CodeProject
www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D 4/9
DataSet ds = new DataSet();sqlDataAdapter1.Fill(ds);
4.3.2.2 Method 2 sqlDataReader
This method is more complex than using the sqlDataAdapters Fill() method, but it allows forpreprocessing of data prior to populating table rows and the data is not bound directly to the database. Inthe example, instead of restricting the database record set to contain distinct Atomic Number rowsthrough a SQL query, it is done programmatically for illustration purposes.
Collapse | Copy Code
string SQL= "SELECT ElementsID, AtomicNbr, " +"Symbol, AtomicMass, Element+ FROM [Elements] order by AtomicNbr ASC" ;sqlConnection sqlConnection1=new sqlConnection ( "connection info to MSDE or SQL Server 2000+;);sqlCommand sqlCommand = new sqlCommand (SQL,sqlConnection1);sqlConnection1.Open();sqlDataReader elementReader = SqlCommand.ExecuteReader();// Starting with the element table dt defined in// the Tables section, an ElementID column// is added that will be used as the primary key for the row.DataColumn dc = new DataColumn(ElementsID, System.Type.GetType(System.Guid));dt.Columns.Add(dc);// Make ElementsID a primary key:
dt.PrimaryKey = new DataColumn[]{dt.Columns[" ElementsID "]};// Fill table dt with data from the database table Elements:
Note: the sqlDataReader class has a method that can be used to determine whether a null or non-existentvalue was returned for a particular cell. For example:
Collapse | Copy Code
If (!elementReader.IsDBNull(elementReader.GetOrdinal( "AtomicNbr" ))){ // fi ll cel l with value}else{ // ha ndle t his co nditio n // fo r exam ple fi ll cel l with a def ault v alue}
This check has been omitted in the following code for clarity, but it is a good practice to use it.
Collapse | Copy CodeDataRow dr;int PrevAtomicNbr = 0 ;try{ while (myReader.Read()) { if (PrevAtomicNbr != elementReader.GetInt32( elementReader.GetOrdinal( "AtomicNbr" ))) { PrevAtomicNbr = elementReader.GetInt32( elementReader.GetOrdinal( "AtomicNbr" )); dr = dt.NewRow(); dr[ElementsID] = elementReader.GetGuid(elementReader.GetOrdinal( "ElementsID" )); dr[AtomicNbr] = elementReader.GetInt32( elementReader.GetOrdinal( "AtomicNbr" )); dr[Symbol] = elementReader.GetString( elementReader.GetOrdinal( "Symbol));
dr[Element] = elementReader.GetString( elementReader.GetOrdinal(" Element ")); dr[AtomicMass] = elementReader.GetDecimal(elementReader.GetOrdinal(" AtomicMass ")); dt.Rows.Add(dr); } }}finally{ elementReader.Close(); sqlConnection1.Close();}dt.AcceptChanges();// Add table dt to a new dataset ds and its tables collectionDataSet ds = new DataSet();ds.Tables.Add(dt);
http://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D# -
8/13/2019 A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 3 Dataset
5/9
1/20/14 A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 3 - CodeProject
www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D 5/9
4.4 Linked TablesThis example shows how to link two tables together through a primary key. In this example a Table withTableName of Elements is created with a Primary key of Atomic Number. The second Table withTableName of Isotopes is linked through a relationship coupling its Atomic Number column to Elementsprimary key.
Collapse | Copy Code
// cre ate a new Da taSet named Perio dic th at wil l // hol d two linked tables with // Tab leNam es of Elemen ts and Isoto pes re specti vely.
DataSet elementDS = new DataSet( "Periodic" ); // Cre ate E lement s Tabl e
DataTable dtElements = new DataTable( "Elements" );dtElements.Columns.Add( "Atomic Number" , typeof ( int ));dtElements.Columns.Add( "Element" , typeof ( string ));dtElements.Columns.Add( "Symbol" , typeof ( string ));
// Mak e At omic N umber a prim ary k ey in Elemen ts tab ledtElements.PrimaryKey = new DataColumn[]{dtElements.Columns[ "Atomic Number" ]};
// Cre ate I sotope s Tabl eDataTable dtIsotopes = new DataTable( "Isotopes" );dtIsotopes.Columns.Add( "Symbol" , typeof ( string ));dtIsotopes.Columns.Add( "Atomic Number" , typeof ( int ));dtIsotopes.Columns.Add( "Isotope Number" , typeof ( int ));dtIsotopes.Columns.Add( "Percent Abundance" ,typeof (System.Decimal))dtIsotopes.Columns.Add( "Atomic Mass" , typeof (System.Decimal));
// Add tables to the Datase tElementDS.Tables.AddRange( new DataTable(){dtElements, dtIsotopes});
Dataset . Relations.Add()
Collapse | Copy Code
// Add a rela tionsh ip for Tables Eleme nts an d Isot opes // thr ough the pr imary key A tomic Number in D ataSet // Per iodic and na me the relati onshi p Iso topes . This name i s // use d in the Da taGrid to sel ect t able r ows, i n tabl e Isot opes, // tha t con tain i sotope values for t he sel ected elemen t.
elementDS.Relations.Add( "Isotopes" ,elementDS.Tables[ "Elements" ].Columns[ "Atomic Number" ],elementDS.Tables[ "Isotopes" ].Columns[ "Atomic Number" ] );
4.5 Linked tables in a dataset
4.5.1 Filling
Assume that another DataSet ds exists that has a table with index 0 in its Tables collection that containsboth Element and Isotope data that will be used to fill rows in the linked tables contained in theelementDS DataSet tables collection as defined in the previous section. Assume Table[0] in DataSet ds hasthe following columns:
AtomicNbr, Element, Symbol, IsotopeNbr, PctAbundance and AtomicMass
where rows are sorted by Atomic numbers ascending and then by IsotopeNbr numbers ascending.
Collapse | Copy Code
// Ass ign t he tab le con tainin g bot h Elem ents a nd // Iso topes to dt using index 0
DataTable dt = ds.Tables[ 0 ]; // Cre ate t wo Dat aTable variab les d tEleme nts an d dtIs otopes and // ass ign t ables contai ned in the D ataSet elemen tDS Ta bles // col lecti on usi ng Tab leName s as indexe s.
DataTable dtElements = elementDS.Tables[Elements];DataTable dtIsotopes = elementDS.Tables[Isotopes];DataRow drElement;DataRow drIsotope;int prevAtomicNbr = 0 ;foreach (DataRow dr in dt.Rows){ if (prevAtomicNbr != ( int )dr[ "AtomicNbr" ]) { //nee d only one ro w per Atomi cNbr i n Tabl e[Ele ments ] // Fi ll an elemen t row with data f rom dt . prevAtomicNbr = ( int )dr[ "AtomicNbr" ]; drElement = dtElements.NewRow(); drElement[ "Atomic Number" ] = dr[ "AtomicNbr" ]; drElement[ "Element" ] = dr[ "Element" ]; drElement[ "Symbol" ] = dr[ "Symbol" ]; dtElements.Rows.Add(drElement);
http://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D# -
8/13/2019 A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 3 Dataset
6/9
1/20/14 A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 3 - CodeProject
www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D 6/9
} // Fi ll an isotop e row with d ata f rom dt . drIsotope = dtIsotopes.NewRow(); drIsotope[ "Isotope Number" ] = dr[ "IsotopeNbr" ]; drIsotope[ "Symbol" ] = dr[ "Symbol" ]; drIsotope[ "Atomic Number" ] = dr[ "AtomicNbr" ]; drIsotope[ "Percent Abundance" ] = dr[ "PctAbundance" ]; drIsotope[ "Atomic Mass" ] = dr[ "AtomicMass" ]; dtIsotopes.Rows.Add(drIsotope);}
4.5.2 Removing
To remove all linked tables or a selected table that is linked from the DataSet , it is first necessary toremove all relations, then constraints and then the table otherwise relationship/constraint table errors aregenerated.
The following code example provides a generic routine for removing all linked tables in a dataset.
Collapse | Copy Code
public void RemoveAllTables(DataSet ds){ // ne ed to do it in rev erse o rder due to constr aints ds.Relations.Clear(); for ( int i=ds.Tables.Count -1; i >=0; i--) { ds.Tables[i].Constraints.Clear(); ds.Tables.RemoveAt(i); }}
4.6 XML Export and Import DataSet Data
4.6.1 WriteXml
All tables with their schemas, relationships, constraints and data contained in a DataSet can be exported inXML by specifying a DataSet property Namespace and using the WriteXml method.
For example:
Collapse | Copy Code
ds.Namespace = "http://www.mydomain.com/xmlfilesds.WriteXml(FileName, XmlWriteMode.WriteSchema);
4.6.2 ReadXml
All tables with their schemas, relationships, constraints and data contained in an XML file are imported intoa DataSet by specifying a DataSet property Namespace and using the ReadXml method. Once in theDataSet it is just like any other dataset.
For example:
Collapse | Copy Code
DataSet ds = new DataSet();ds.Namespace = "http://www.mydomain.com/xmlfiles" ;ds.ReadXml(FileName, XmlReadMode.ReadSchema);
4.7 Handling DataSet ErrorsSimilar to the DataTable HasErrors property the DataSet HasErrors property returns true if any errorsoccurred in any of the tables being managed by the DataSet.
Collapse | Copy Code
if (ds.HasErrors){ // On e or m ore of the ta bles i n the DataSe t has errors . MessageBox.Show( "DataSet has Errors" ); // In sert c ode to resolv e erro rs. // Re fer to Handl ing Da taTabl e Err ors s ection for ex ample of // ha ndling errors within tables .}
http://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D# -
8/13/2019 A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 3 Dataset
7/9
1/20/14 A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 3 - CodeProject
www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D 7/9
Pete2004President BioxingUnited 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 edgeinstrumentation and data systems. These include DNA and Peptide Synthesizers, cDNA, oligo andProtein microarrays and mass spectrometers used for protein sequencing.In 2001 foundedBioXing (pronounced Bio-Crossing) which has architected and developed an extensible relationaldatabase repository and software system that includes Web Services, Client Workstation and WebBased applications. The system is used to track, manage, integrate and data mine disparatelaboratory data, protocols and experiments and link to reference proteomic and genomic data.BioXing also does consulting and development for biotechnology companies.
4.8 Updating Database with DataSet/DataTablechangesThe following code shows how to create a DataSet containing all of the changes that have occurred totables within a DataSet. The new DataSet can be used for updating the database.
Collapse | Copy Code
// Cre ate t empora ry Dat aSet v ariab le.DataSet dsChanges;
// Get Chang es for modifi ed row s onl y.dsChanges = ds.GetChanges(DataRowState.Modified); // Che ck th e Data Set fo r erro rs.
if (!dsChanges.HasErrors){ // No errors were f ound, update the DB MS wit h the SqlDat aAdap ter da // us ed to create the Da taSet. da.RowUpdating += new SqlRowUpdatingEventHandler(OnRowUpdating); da.RowUpdated += new SqlRowUpdatedEventHandler(OnRowUpdated); int res = da.Update(dsChanges); // ret urns t he num ber of rows update d da.RowUpdating -= new SqlRowUpdatingEventHandler(OnRowUpdating); da.RowUpdated -= new SqlRowUpdatedEventHandler(OnRowUpdated);}
Next
Data Grids
LicenseThis article has no explicit license attached to it but may contain usage terms in the article text or thedownload files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
About the Author
Article Top
http://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#_articleTophttp://www.codeproject.com/info/Licenses.aspxhttp://www.codeproject.com/KB/grid/PracticalGuideDataGrids4.asphttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://adclick.g.doubleclick.net/aclk?sa=L&ai=Bq77K5hvdUs3YBOa5igej74CIBQAAAAAQASAAOABQqryNUVj__K0aYOWCgIC8DoIBCWNhLWdvb2dsZbIBGHd3dy5kY2xrLWRlZmF1bHQtcmVmLmNvbcgBAtoBIGh0dHA6Ly93d3cuZGNsay1kZWZhdWx0LXJlZi5jb20vqAMB4AQCmgUZCJGCVBCEobgyGIvi54QBIP_8rRooybfkAdoFAggBoAYU4AbJt-QB&num=0&sig=AOD64_1jim-gn3cqlPIaa6ep_4SB_QqdJQ&client=&adurl=http://www.newrelic.comhttp://www.codeproject.com/Members/Pete2004 -
8/13/2019 A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 3 Dataset
8/9
1/20/14 A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 3 - CodeProject
www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D 8/9
Search this forum Go
Comments and Discussions
You must Sign In to use this message board.
Profile popups Spacing Relaxed Noise Very High Layout Normal Per page 50 Update
First Prev Next
Bolud 13-Aug-10 12:14
pentel 27-Aug-08 19:45
immetoz 4-Oct-08 2:02
Pete2004 6-Oct-08 6:04
HinJinShah 1-Aug-07 5:50
Pete2004 1-Aug-07 6:04
HinJinShah 3-Aug-07 9:38
Pete2004 4-Aug-07 14:56
HinJinShah 8-Aug-07 6:43
rjorget 2-Feb-06 8:13
DharmarajNagarajan 13-Nov-07 23:18
Mr.Bill 16-Aug-05 2:13
Pete2004 17-Aug-05 10:45
Miroka 8-Jun-05 7:06
Holger Flick 31-Jul-04 13:21
Pete2004 17-Aug-05 10:48
Anonymous 10-Jul-04 9:00
partyganger 10-Jul-04 13:09
Anonymous 15-Jul-04 19:32
Pete2004 15-Jul-04 6:34
Anonymous 28-May-04 11:34
Pete2004 28-May-04 12:07
sjkdo 17-May-04 6:27
Pete2004 17-May-04 7:08
sjkdo 17-May-04 11:18
EstimatedProphet 4-May-04 3:32
Pete2004 5-May-04 4:33
EstimatedProphet 5-May-04 4:40
Pete2004 5-May-04 5:40
EstimatedProphet 5-May-04 5:45
Pete2004 5-May-04 5:55
My vote of 5
Good article but unnecessary complex
[Message Removed]
Re: Good article but unnecessary complex
Updating Dataset Problems
Re: Updating Dataset Problems
Re: Updating Dataset Problems
Re: Updating Dataset Problems [modified]
Re: Updating Dataset Problems
how read a xml with dataset class
Re: how read a xml with dataset class
InsertAt() causes errors
Re: InsertAt() causes errors
conversion excel file into txt file
WriteXML and blob
Re: WriteXML and blob
Programatic DataGrid Scrolling
Re: Programatic DataGrid Scrolling
Re: Programatic DataGrid Scrolling
Re: Programatic DataGrid Scrolling
ReadXML and datagrid
Re: ReadXML and datagrid
A problem with WriteXml
Re: A problem with WriteXml
Re: A problem with WriteXml
Changing Column Order
Re: Changing Column Order
Re: Changing Column Order
Re: Changing Column Order
Re: Changing Column Order
Re: Changing Column Order
http://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/812904/Re-Changing-Column-Order.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/812893/Re-Changing-Column-Order.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/812880/Re-Changing-Column-Order.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/812750/Re-Changing-Column-Order.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/812739/Re-Changing-Column-Order.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/811185/Changing-Column-Order.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/824608/Re-A-problem-with-WriteXml.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/824335/Re-A-problem-with-WriteXml.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/824278/A-problem-with-WriteXml.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/836324/Re-ReadXML-and-datagrid.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/836312/ReadXML-and-datagrid.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/876388/Re-Programatic-DataGrid-Scrolling.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/876816/Re-Programatic-DataGrid-Scrolling.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/872380/Re-Programatic-DataGrid-Scrolling.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/872317/Programatic-DataGrid-Scrolling.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/1198078/Re-WriteXML-and-blob.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/886804/WriteXML-and-blob.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/1130246/conversion-excel-file-into-txt-file.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/1198076/Re-InsertAt-causes-errors.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/1196233/InsertAt-causes-errors.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/2318006/Re-how-read-a-xml-with-dataset-class.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/1357013/how-read-a-xml-with-dataset-class.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/2173716/Re-Updating-Dataset-Problems.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/2168057/Re-Updating-Dataset-Problems-modified.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/2167150/Re-Updating-Dataset-Problems.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/2162627/Re-Updating-Dataset-Problems.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/2162599/Updating-Dataset-Problems.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/2753311/Re-Good-article-but-unnecessary-complex.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/2751019/Message-Removed.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/2697389/Good-article-but-unnecessary-complex.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/3565024/My-vote-of-5.aspxhttp://www.codeproject.com/script/Membership/View.aspx?mid=801806http://www.codeproject.com/script/Membership/View.aspx?mid=1042706http://www.codeproject.com/script/Membership/View.aspx?mid=801806http://www.codeproject.com/script/Membership/View.aspx?mid=1042706http://www.codeproject.com/script/Membership/View.aspx?mid=801806http://www.codeproject.com/script/Membership/View.aspx?mid=1042706http://www.codeproject.com/script/Membership/View.aspx?mid=131156http://www.codeproject.com/script/Membership/View.aspx?mid=801806http://www.codeproject.com/script/Membership/View.aspx?mid=131156http://www.codeproject.com/script/Membership/View.aspx?mid=801806http://www.codeproject.com/script/Membership/View.aspx?mid=801806http://www.codeproject.com/script/Membership/View.aspx?mid=253914http://www.codeproject.com/script/Membership/View.aspx?mid=801806http://www.codeproject.com/script/Membership/View.aspx?mid=1200934http://www.codeproject.com/script/Membership/View.aspx?mid=2029233http://www.codeproject.com/script/Membership/View.aspx?mid=801806http://www.codeproject.com/script/Membership/View.aspx?mid=372967http://www.codeproject.com/script/Membership/View.aspx?mid=2891329http://www.codeproject.com/script/Membership/View.aspx?mid=2421291http://www.codeproject.com/script/Membership/View.aspx?mid=661178http://www.codeproject.com/script/Membership/View.aspx?mid=801806http://www.codeproject.com/script/Membership/View.aspx?mid=661178http://www.codeproject.com/script/Membership/View.aspx?mid=801806http://www.codeproject.com/script/Membership/View.aspx?mid=661178http://www.codeproject.com/script/Membership/View.aspx?mid=801806http://www.codeproject.com/script/Membership/View.aspx?mid=5567521http://www.codeproject.com/script/Membership/View.aspx?mid=5460217http://www.codeproject.com/script/Membership/View.aspx?mid=6604644https://www.codeproject.com/script/Membership/LogOn.aspx?rp=%2fArticles%2f6180%2fA-Practical-Guide-to-NET-DataTables-DataSets-and-D%3ffid%3d33941 -
8/13/2019 A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 3 Dataset
9/9
1/20/14 A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 3 - CodeProject
wwwcodeproject com/Articles/6180/A Practical Guide to NET DataTables DataSets and D 9/9
Permalink | Advertise | Privacy | Mobile Web02 | 2.8.140119.1 | Last Updated 24 Feb 2004
Article Copyright 2004 by Pete2004Everything else Copyright CodeProject , 1999-2014
Terms of Us
Layout: fixed | fluid
Mike Osbahr 27-Jul-04 10:55
Anonymous 9-Aug-04 12:56
dothedew 1-Nov-05 6:26
Boyan Botev 29-Apr-04 2:15
Pete2004 29-Apr-04 8:03
Pete2004 30-Apr-04 15:03
MikeBeard 25-Feb-04 5:15
Pete2004 29-Apr-04 12:19
Last Visit: 31-Dec-99 18:00 Last Update: 19-Jan-14 21:51 Refresh 1
General News Suggestion Question Bug Answer Joke Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
Re: Changing Column Order
Re: Changing Column Order
Re: One Solution for Changing Column Order
Very useful article.
Re: Very useful article.
Re: Very useful article.
Link your articles together?
Re: Link your articles together?
http://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/806963/Re-Link-your-articles-together.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/748923/Link-your-articles-together.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/808187/Re-Very-useful-article.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/806706/Re-Very-useful-article.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/806283/Very-useful-article.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/1269867/Re-One-Solution-for-Changing-Column-Order.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/894121/Re-Changing-Column-Order.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D#http://www.codeproject.com/Messages/883742/Re-Changing-Column-Order.aspxhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D?fid=33941http://www.codeproject.com/script/Membership/View.aspx?mid=801806http://www.codeproject.com/script/Membership/View.aspx?mid=3042http://www.codeproject.com/script/Membership/View.aspx?mid=801806http://www.codeproject.com/script/Membership/View.aspx?mid=801806http://www.codeproject.com/script/Membership/View.aspx?mid=1016329http://www.codeproject.com/script/Membership/View.aspx?mid=1184154http://www.codeproject.com/script/Membership/View.aspx?mid=17282http://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D?PageFlow=Fluidhttp://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D?PageFlow=FixedWidthhttp://www.codeproject.com/info/TermsOfUse.aspxmailto:[email protected]://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D?display=Mobilehttp://www.codeproject.com/info/privacy.aspxhttp://developermedia.com/http://www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D