Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Navigating through a DataTable 1

Status
Not open for further replies.

FOR111

Programmer
Sep 29, 2005
103
MT
Hi All,

I have loaded a DataTable with a set of records. Now i would like to go thorugh them as i used to do with the VB6 style ADO using MoveNext, MovePrevious etc...

I can navigate to rows using: MyDataTable.Rows[0] etc but i don;t know how i can retrive the current row so i can move from there...!!

Any help would be most welcome!

Thanks
Nick
 
Code:
foreach(DataRow r in myDataTable.Rows)
	{
		Response.Write("Some Value: " + r["SomeColumnName"].ToString() + "<br/>");
	}
or
Code:
for(int i = 0; i < myDataTable.Rows.Count; i++)
	{
		Response.Write("Some Value: " + myDataTable.Rows[i]["SomeColumnName"].ToString() + "<br/>");
	}
 
Hi VEEP, thanks for your reply,

The code above looks good, only if you want to list the whole result set!

What i want is to know when I'm creating a function named MoveNext how i would know my present location within the the table!

ex. Current Record: 4

MoveNext (5)


Thanks
Nick
 
a datatable doesn't have any concept of "current", it's just a table of data.
to do what you want in the way you want, you have to keep a record pointer yourself.

so:

Code:
private int currentRecord = 0;

public void MoveNext()
{
  currentRecord++;
}

public void somefunction()
{
  doSomethingWith(dataTable.Rows[currentRecord]["column"]);
  MoveNext();
  // now at the next record
  doSomethingWith(dataTable.Rows[currentRecord]["column"]);
}

obviously this opens up a whole shelfful of tins of worms.

a better, and more .net way of doing it, is to use a CurrencyManager.

Code:
private CurencyManager myCM;

public void MoveNext()
{
  myCM.Position++;
}

public void somefunction()
{
  myCM = (CurrencyManager)this.BindingContext[dataTable];
  doSomethingWith(dataTable.Rows[myCm.Position]["column"]);
  MoveNext();
  // now at the next record
  doSomethingWith(dataTable.Rows[myCm.Position]["column"]);
}

this will deal with all sorts of stuff for you, like BOF and EOF checking.

hope this helps,

mr s. <;)

 
MrS..thanks! i think i can work out with that.

Just for my personal info (as i stated i'm quite new to c#) what is the use of the CurrencyManager?

Also, one last question:

Is the first Row indicated by the index 0 or 1?

Thanks once again
Nick
 
as far as i know, internal arrays in c# are always zero based.

all a currency manager is for is keeping tabs on where the current record is. for instance, datagrids have one when they're bound to a dataview-type object.

good luck,

mr s. <;)

 
cheers

and thanks once again!


Nick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top