You are on page 1of 5

DataTable - Adding, Modifying, Deleting, Filtering, Sorting rows & Read...

http://www.dotnetfunda.com/articles/article131.aspx

Home

Freshers

Career Advice

Articles

Interviews

Videos

Forums

Codes

Blogs

Jobs

Job Support

Community

Jokes

Chat

MVPs | ASP.NET, WPF, SSIS etc. tutorials | Catalog | Coding Horror | Downloads | Quick Links | Top Posts | Top Authors | .NET News | Bookmarks | Search | Subscribe

Online : 12238 | Welcome, Guest! Register Login Home > Articles > ADO.NET > DataTable - Adding, Modifying, Deleting, Filtering, Sorting rows & Reading/Writing from/to Xml Also read How to Serialize and DeSerialize an ArrayList having a custom object inside it | ListView Tips and Tricks | Generating XML from relational database tables

DataTable - Adding, Modifying, Deleting, Filtering, Sorting rows & Reading/Writing from/to Xml
Article posted by SheoNarayan on 9/3/2008 | Views: 365933 | Category: ADO.NET | Level: Intermediate
Tweet 26 Share 1 12

4 vote(s)
Rating: 5 out of 5

Submit Article |

Search Articles |

Articles Home |

Winners

In this article, I am going to explain how to Add, Modify, Delete, Sort, Filter rows of the DataTable and also loading data from xml and writing data into xml. I will also talk about writing/reading Schema of the DataTable.

Introduction
DataTable is a central object in the ADO.NET library. If you are working with ADO.NET - accessing data from database, you can not escape from DataTable. Other objects that use DataTable are DataSet and DataView. In this tutorials, I will explain how to work with DataTable. I have tried to cover most of the frequently used activity in the DataTable, I hope you will like it.

Winners & Prizes Announcements Like us on Facebook


Facebook Like 3,614 people like DotNetFunda.

DotNetFunda on

Creating a DataTable
To create a DataTable, you need to use System.Data namespace, generally when you create a new class or page, it is included by default by the Visual Studio. Lets write following code to create a DataTable object. Here, I have pased a string as the DataTable name while creating DataTable object.

Avdhesh

Jitender

Ashish

Facebook social plugin

Top Articles Authors

Wed, 23-May-2012 Authors All Time Authors

// instantiate DataTable DataTable dTable = new DataTable("Dynamically_Generated");

31050

12250

10000

Creating Columns in the DataTable


To create column in the DataTable, you need to use DataColumn object. Instantiate the DataColumn object and pass column name and its data type as parameter. Then call add method of DataTable column and pass the DataColumn object as parameter. // create columns for the DataTable DataColumn auto = new DataColumn("AutoID", typeof(System.Int32)); dTable.Columns.Add(auto); // create another column DataColumn name = new DataColumn("Name", typeof(string)); dTable.Columns.Add(name); // create one more column DataColumn address = new DataColumn("Address", typeof(string)); dTable.Columns.Add(address);

Latest members | More ...


(Statistics delayed by 5 minutes)

Ads

Specifying AutoIncrement column in the DataTable


To specify a column as AutoIncrement (naturally it should be an integer type of field only), you need to set some properties of the column like AutoIncrement, AutoIncrementSeed. See the code below, here I am setting the first column "AutoID" as autoincrement field. Whenever a new row will be added its value will automatically increase by 1 as I am specified AutoIncrementSeed value as 1.

1 of 5

5/23/2012 1:27 PM

DataTable - Adding, Modifying, Deleting, Filtering, Sorting rows & Read...

http://www.dotnetfunda.com/articles/article131.aspx

// specify it as auto increment field auto.AutoIncrement = true; auto.AutoIncrementSeed = 1; auto.ReadOnly = true; If you want a particular column to be a unique column ie. you don't want duplicate records into that column, then set its Unique property to true like below. auto.Unique = true;

Specifying Primary Key column in the DataTable


To set the primary key column in the DataTable, you need to create arrays of column and store column you want as primary key for the DataTable and set its PrimaryKey property to the column arrays. See the code below. // create primary key on this field DataColumn[] pK = new DataColumn[1]; pK[0] = auto; dTable.PrimaryKey = pK; Till now we have created the DataTable, now lets populate the DataTable with some data.

Populating data into DataTable


There are two ways to populate DataTable. Using DataRow object Look at the code below, I have created a DataRow object above the loop and I am assiging its value to the dTable.NewRow() inside the loop. After specifying columns value, I am adding that row to the DataTable using dTable.Rows.Add method. // populate the DataTable using DataRow object DataRow row = null; for (int i = 0; i < 5; i++) { row = dTable.NewRow(); row["AutoID"] = i + 1; row["Name"] = i + " - Ram"; row["Address"] = "Ram Nagar, India - " + i; dTable.Rows.Add(row); } Instead of using the column name, you can use ColumnIndex too, however it is not suggested as you might want to add a column in the mid of the table then you will need to change your code wherever you have specified the index of the column. Same applies while reading or writing values into Database column. Asiging the value of column using Arrays In following code, I have specified the values of every column as the array separated by comma (,) in the Add method of the dTable.Rows. // manually adding rows using array of values dTable.Rows.Add(6, "Manual Data - 1", "Manual Address - 1, USA"); dTable.Rows.Add(7, "Manual Data - 2", "Manual Address - 2, USA");

Articles Categories .NET Framework Articles ADO.NET Articles ASP.NET Articles ASP.NET AJAX Articles ASP.NET MVC Articles Azure Articles Best Practices Articles BizTalk Server Articles C# Articles CMS Articles CSS Articles Error and Resolution Articles F# Articles Advertisements

Modifying data into DataTable


Modifying Row Data To edit the data of the row, sets its column value using row index or by specifying the column name. In below example, I am updating the 3rd row of the DataTable as I have specified the row index as 2 (dTable.Rows[2]). // modify certain values into the DataTable dTable.Rows[2]["AutoID"] = 20; dTable.Rows[2]["Name"] = "Modified"; dTable.Rows[2]["Address"] = "Modified Address"; dTable.AcceptChanges(); Deleting Row To delete a row into DataTable, call the rows.Delete() method followed by AcceptChanges() method. AcceptChanges() method commits all the changes made by you to the DataTable. Here Row[1] is the index of the row, in this case 2nd row will be deleted as in collection (here rows collection) count start from 0. // Delete row dTable.Rows[1].Delete(); dTable.AcceptChanges();

2 of 5

5/23/2012 1:27 PM

DataTable - Adding, Modifying, Deleting, Filtering, Sorting rows & Read...

http://www.dotnetfunda.com/articles/article131.aspx

Filtering data from DataTable


To filter records from the DataTable, use Select method and pass necessary filter expression. In below code, the 1st line will simply filter all rows whose AutoID value is greater than 5. The 2nd line of the code filters the DataTable whose AutoID value is greater than 5 after sorting it. DataRow[] rows = dTable.Select(" AutoID > 5"); DataRow[] rows1 = dTable.Select(" AutoID > 5", "AuotID ASC"); Note that Select method of the DataTable returns the array of rows that matche the filter expression. If you want to loop through all the filtered rows, you can use foreach loop as shown below. In this code, I am adding all the filtered rows into another DataTable. foreach (DataRow thisRow in rows) { // add values into the datatable dTable1.Rows.Add(thisRow.ItemArray); } Working with Aggregate functions (Updated on 18-Nov-08) We can use almost all aggregate functions with DataTable, however the syntax is bit different than standard SQL. Suppose we need to get the maximum value of a particular column, we can get it in the following way. DataRow[] rows22 = dTable.Select("AutoID = max(AutoID)"); string str = "MaxAutoID: " + rows22[0]["AutoID"].ToString(); To get the sum of a particular column, we can use Compute method of the DataTable. Compute method of the DataTable takes two argument. The first argument is the expression to compute and second is the filter to limit the rows that evaluate in the expression. If we don't want any filteration (if we need only the sum of the AutoID column for all rows), we can leave the second parameter as blank (""). object objSum = dTable.Compute("sum(AutoID)", "AutoID > 7"); string sum = "Sum: " + objSum.ToString(); // To get sum of AutoID for all rows of the DataTable object objSum = dTable.Compute("sum(AutoID)", "");

Sorting data of DataTable


Oops !. There is no direct way of sorting DataTable rows like filtering (Select method to filter DataRows). There are two ways you can do this. Using DataView See the code below. I have created a DataView object by passing my DataTable as parameter, so my DataView will have all the data of the DataTable. Now, simply call the Sort method of the DataView and pass the sort expression. Your DataView object have sorted records now, You can either directly specify the Source of the Data controls object like GridView, DataList to bind the data or if you need to loop through its data you can use ForEach loop as below. // Sorting DataTable DataView dataView = new DataView(dTable); dataView.Sort = " AutoID DESC, Name DESC"; foreach (DataRowView view in dataView) { Response.Write(view["Address"].ToString()); } Using DataTable.Select() method Yes, you can sort all the rows using Select method too provided you have not specified any filter expression. If you will specify the filter expression, ofcourse your rows will be sorted but filter will also be applied. A small drawback of this way of sorting is that it will return array of DataRows as descibed earlier so if you are planning to bind it to the Data controls like GridView or DataList you will have for form a DataTable by looping through because directly binding arrays of rows to the Data controls will not give desired results. DataRow[] rows = dTable.Select("", "AutoID DESC");

Writing and Reading XmlSchema of the DataTable


If you need XmlSchema of the DataTabe, you can use WriteXmlSchema to write and ReadXmlSchema to read it. There are several overloads methods of both methods and you can pass filename, stream, TextReader, XmlReader etc. as the parameter. In this code, the schema will be written to the .xml file and will be read from there. // creating schema definition of the DataTable dTable.WriteXmlSchema(Server.MapPath("~/DataTableSchema.xml")); // Reading XmlSchema from the xml file we just created DataTable dTableXmlSchema = new DataTable(); dTableXmlSchema.ReadXmlSchema(Server.MapPath("~/DataTableSchema.xml"));

3 of 5

5/23/2012 1:27 PM

DataTable - Adding, Modifying, Deleting, Filtering, Sorting rows & Read...

http://www.dotnetfunda.com/articles/article131.aspx

Reading/Writing from/to Xml


If you have a scenario, where you need to write the data of the DataTable into xml format, you can use WriteXml method of the DataTable. Note that WriteXml method will not work if you will not specify the name of the DataTable object while creating it. Look at the first code block above, I have passed "Dynamically_Generated" string while creating the instance of the DataTable. If you will not specify the name of the DataTable then you will get error as WriteXml method will not be able to serialize the data without it. // Note: In order to write the DataTable into XML, // you must define the name of the DataTable while creating it // Also if you are planning to read back this XML into DataTable, you should define the XmlWriteMode.WriteSchema too // Otherwise ReadXml method will not understand simple xml file dTable.WriteXml(Server.MapPath("~/DataTable.xml"), XmlWriteMode.WriteSchema); // Loading Data from XML into DataTable DataTable dTableXml = new DataTable(); dTableXml.ReadXml(Server.MapPath("~/DataTable.xml")); If you are planning to read the xml you have just created into the DataTable sometime later then you need to specify XmlWriteMode.WriteSchema too as the 2nd parameter while calling WriteXml method of the DataTable otherwise normally WriteXml method doesn't write schema of the DataTable. In the abscence of the schema, you will get error (DataTable does not support schema inference from Xml) while calling ReadXml method of the DataTable.

Hope this article will be useful. If you have any question, comments or suggestions, please respond to this article. Thank you very much for reading it and Happy DataTable :)
.NET web programming Simplify your .NET web programming in C# or VB, with Jaxcent. www.jaxcent.com/ Chart Control for ASP.NET Full-Featured Web Charting Solution for ASP.NET Ajax Platform www.devexpress.com Image resizing on the fly Make high-quality image thumbnails with this ASP/ASP.NET component. www.aspjpeg.com

If you like this article, subscribe to our Forums section.

RSS Feed. You can also subscribe via email to our Interview Questions, Codes and

Found interesting? Add this to:


Tweet 26 Share 1 12

More | Bookmark It

Please Sign In to vote for this post. Latest Articles from SheoNarayan Customizing asp:Chart Label font styles and intervals etc. How to use Rotating, Skew and Multiple background image in CSS3? How to use Opacity, Scale and box shadow and Zooming in CSS 3? How to use Transition,Text-Shadow and Border-Radius effect in CSS 3? How to play audio in the HTML5 and how to control the audio play using in HTML element ? More from SheoNarayan ...

About Sheo Narayan


Experience: Home page: Member since: Level: Status: Biography: 8 year(s) http://www.snarayan.com Tuesday, July 08, 2008 HonoraryPlatinum [Microsoft_MVP] [Administrator] Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on Facebook | Twitter | LinkedIn | Blog

Responses
Posted by: Ankit.dadhich@yahoo.com | Posted on: 08 Jul 2010 02:47:28 AM thanks very helpful Posted by: Fxjamy | Posted on: 06 Sep 2010 02:43:49 AM | Points: 10 Hello, I was wondering if it is possible to style particular columns? Need to show Money symbol in some columns. Thanks Posted by: Lakhwinder.ghuman | Posted on: 23 May 2011 03:45:00 AM | Points: 25 too good.

4 of 5

5/23/2012 1:27 PM

DataTable - Adding, Modifying, Deleting, Filtering, Sorting rows & Read...

http://www.dotnetfunda.com/articles/article131.aspx

Posted by: G | Posted on: 07 Jun 2011 10:29:50 PM | Points: 25 Following is the code that i currently have as an output from dataset. I used ds.writexml("FILENAME"); Cool. <SalesTransactions> <Transaction> <sale_tx_no>1002367</sale_tx_no> <sale_date>2008-01-14T15:37:07+13:00</sale_date> <loyalty_card>234234234234</loyalty_card> </Transaction> <SalesTransactions> But I want to change the names of allthe columns in this xml file as under: <SalesTransactions> <Transaction> <Reference>1002367</Reference> <DateofSale>2008-01-14T15:37:07+13:00</DateofSale> <Card>234234234234</Card> </Transaction> <SalesTransactions> Can someone help me out!! Posted by: TitoChhabra | Posted on: 15 Oct 2011 03:26:53 AM | Points: 25 Hi, Great job. Nice article. It's really very helpful for me. Here is also another nice article on datatable, you may check this url for more details ...... http://www.mindstick.com/Articles/4d0f2113-b3e8-4516-90f5-028adeaae201/?DataTable Thanks !! >> Write Response - Respond to this post and get points Latest Articles How to use String.Format function of C# in Javascript and display multiple values of a resource file. Working with Silverlight Datagrid Control MVC tutorial number 12 - What is RAZOR in MVC 3? Creating a circle with CSS How to validate using Data annotation(MVC tutorial number 11)? Invoke Javascript Methods from Silverlight Applications Invoke Silverlight Methods through Javascript How to create partial views (MVC tutorial number 10)? Create Chart in SSRS(RDLC) Report More ...

Related Posts
ADO.NET Interview Questions - Part 1 This is the Part 1 of ADO.NET. In this section we will touch base on one of important concepts in ADO.Net. Diff Between DataGrid and GridView Im going to expail the various difference between GridView and the DataGrid controls and their different logics. How to insert records into the database? In this article, we shall learn how to insert records into the SQL Server database. How to use transactions in ADO.NET? In this article, we shall learn how to use transactions in ADO.NET. CRUD Operations using ADO.net Entity Framework In this post I show you how to perform CRUD operations using ADO.net Entity Framework. A sample WPF application is available for download which shows this functionality More ... About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/23/2012 3:22:48 AM

5 of 5

5/23/2012 1:27 PM

You might also like