Forum guidelines
Loop through DataTable in C#
Posted By Amit Maheshwari on 04 Feb 2012
Hi friends,

I want to know that how to loop through all rows of the DataTable?

Re: Loop through DataTable in C#
Posted By Vulpes on 04 Feb 2012  
If 'dt' is your datatable, you can loop through its rows using a foreach statement as follows:

foreach(DataRow dr in dt.Rows)
   // do something with dr

You can also use an ordinary 'for' statement which is useful if you need to know the index of the rows:

for(int i = 0; i < dt.Rows.Count; i++)
    DataRow dr = dt.Rows[i];
    // do something with dr

C# Corner Annual Conference 2016


Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.