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


JetBrains ReSharper Ultimate
.NET code analysis, refactorings, navigation, unit testing, code coverage and profiling in Visual Studio, a bonus for C++ devs, bundled at 50% off.