Saturday 21 April 2012

all rows of the DataTable


How to loop through all rows of the DataTable?


You can do this in more than one way but ForEach loop is much better than any other way in terms of cleanliness of the code or performance.

ForEach loop
foreach (DataRow row in dTable.Rows)

{

     yourvariable = row["ColumnName"].ToString();

}


For loop
for (int j = 0; j< dTable.Rows.Count; j++)

{

    yourvariable = dTable.Rows[j]["ColumnName"].ToString()l

}

No comments:

Post a Comment