Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: ASP.NETC#4.0
I have a 10 rows in my DataTable. Now i want to insert records using foreach loop.
like this
foreach(DataRow dr dt.Rows)
But i don't want to insert row 0.
I want to insert from row 1 onwords.
I know the logic using for loop but i want using foreach.
Can any one give me suggestion to fix the possition using foreach only...
Posted 14-Feb-13 0:19am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

A foreach loop always works on all the elements on the Collection you feed it, so there is no mechanism to "skip" the first (or any other element) other than using break to exit the loop completely.
There are two ways to miss out the first element:
1) Copy the list into a new collection and remove the first element.
2) Add a bool outside the foreach:
bool skip = true;
foreach (DataRow row in dt.Rows)
    if (!skip)
    skip = false;
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

use for loop which start from 1 to your dt.rows.count
naveensenagasetti at 14-Feb-13 7:03am
I already mentioned in my question i know how to do using for loop but i'm asking using foreach loop
vishal.shimpi at 14-Feb-13 7:58am
bool isfirst = true; foreach (DataRow row in dt.Rows) { if(isfirst) { first = false; continue; } //do your operation here }

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
0 Shai Vashdi 1,588
1 OriginalGriff 528
2 Manas Bhardwaj 383
3 Damith Weerasinghe 330
4 Tadit Dash 315
0 Sergey Alexandrovich Kryukov 9,575
1 OriginalGriff 6,006
2 Peter Leow 4,500
3 Maciej Los 3,540
4 Abhinav S 3,513

Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 14 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid