Congratulations to all the winners of April 2013, they have won INR 3400 cash and INR 20147 worth prizes !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 764 |  Welcome, Guest!   Register  Login
Home > Articles > C# > Looping through all rows of the DataTable

Looping through all rows of the DataTable

2 vote(s)
Rating: 5 out of 5
Article posted by SheoNarayan on 10/8/2007 | Views: 59066 | Category: C# | Level: Beginner red flag


Here is the working example of looping through all rows of the DataTable. You can download the code and simply run it by placing to your IIS root folder.

Let me explain you looping through all rows of the DataTable step wise.

Download


 Download source code for Looping through all rows of the DataTable






Namespace to Use


using System.Data;

Steps


First lets place an asp:Literal control on your page. This control will be used to write the rows values as a string. (You may use asp:Label control too but literal best suits when you need to write raw html from server side)


Now get the data into DataTable using any of the DataSource. In this example I have used one .xml file.

Finally, write ForEach loop and manipulate the rows in whatever way you want.


Following is the function that will loop through the DataTable and print the data as displayed into the picture above.


 private void LoadDataTable()

{
string str = string.Empty;
string path = Server.MapPath("~/GridData.xml");
int i = 0;
DataSet dSet = new DataSet();
dSet.ReadXml(path);
DataTable dTable = dSet.Tables[0];
foreach (DataRow dRow in dTable.Rows)
{
i++;
str += "ROW " + i.ToString() + " : <b>Name: </b>" + dRow["FirstName"].ToString() + " " + dRow["LastName"].ToString() + ", <b>Address: </b>"+ dRow["Address"].ToString() +", <b>Profession: </b>" + dRow["Profession"].ToString() + "<br />";
}
litText.Text = str;
}



In the above function, I am declaring one dataset and getting data from .xml file using ReadXml method. Storing that datatable into a local DataTable variable. Then writing ForEach loop to loop through all the rows of the DataTable and storing into the string variabbles and finally specifying the .text property of the asp:literal control.

The working example is attached with this article, don't forget to download if you are facing any problem.

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About Sheo Narayan

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

Connect me on Facebook | Twitter | LinkedIn | Blog

 Responses
Posted by: Akiii | Posted on: 07 Apr 2011 12:18:55 PM | Points: 25

crystal clear explanation....
thank you sir....

Akiii

>> Write Response - Respond to this post and get points
Related Posts

Parameters in C#: In C Sharp(C#) we can have three types of parameters in a function. The parameters can be In parameter (which is not returned back to the caller of the function), Out parameter and ref parameter (where by a reference to the variable is passed back).

This article will discuss about Access Modifier.

SMO allows you to manage all objects which lives in the Microsoft SQL Server. It allows you to create, drop or alter objects from your .NET application with a API which is really easy to use.

In this article we will be exploring the RDLC reporting feature of .NET.In VS 2010 Crystal Reports are not supported and to create Reporting Systems we can use rdlc feature.

I am using two for loops to print stars in differnt shapes.

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 find 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/19/2013 11:36:13 AM