Click here to Skip to main content
15,918,049 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to move to next row in while loop in table

What I have tried:

C#
while (rows["Token_no"].ToString() == cToken && DateTime.Parse(rows["Date"].ToString()) == dDDate)
{
  rows.BeginEdit();
  rows["In_out"] = "I";
  rows.EndEdit();

  // want move to next row
}
Posted
Updated 28-May-17 9:58am
v2
Comments
Kornfeld Eliyahu Peter 28-May-17 8:31am    
What 'rows' is? And what do you mean by rows['']? Have you compiled this code?

Something like this:
foreach (DataRow row in rows)
{
	if (row["Token_no"].ToString() == cToken && DateTime.Parse(row["Date"].ToString()) == dDDate)
	{
		row["In_out"] = "I";
	}
}
Here is an interesting article about working with DataTable and DataRow:[^]

If you have a DataTable table, then you can use:
C#
While (i < table.Rows.Count)
{
   DataRow row = table.Rows[i];
   i++;
}
 
Share this answer
 
v3
Comments
Member 12931315 28-May-17 12:44pm    
Thank you very much for reply

Sorry I didn't put whole code , It's already in foreach loop
there I take token_no and date to while loop for token_no because, there are several token_no's in a day ,once I worked with current one, I want move next line (record)
Is that possible in while loop

Thanks

Ajith
Another way (as an alternative to solution 1 by RickZeeland[^]) to update data is to use Linq.

Take a look at example:
C#
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("ID", typeof(int)));
dt.Columns.Add(new DataColumn("In_out", typeof(string)));
for(int i=0; i<50; i++)
{
	dt.Rows.Add(new Object[]{i, string.Format("row{0}",i)});
}


var query = dt.AsEnumerable()
    .Where(r=>r.Field<int>("ID") % 2 ==0)
    .ToList();

foreach (DataRow row in query)
{
    row.SetField("In_out", "I");
}


Result:
ID In_out
0  I 
1  row1 
2  I 
3  row3 
4  I 
5  row5 
6  I


Change the code to your needs.
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900