Filter rows in a DataSet

H

Hardy Wang

Hi all:
I have a DataSet populated with values. How can I return
ds.Tables[0].DefaultView's records from a starting number to a ending
number?
For example, I have 100 records in the DefaultView, but I just need
records from 25th to 50th.

Thanks for any suggestion!

--



WWW: http://hardywang.1accesshost.com
ICQ: 3359839
yours Hardy
 
R

Ralph Gerbig

hi,

use a for-loop and start at 25 and end at 50 e.g (not tested code)

for (int = 0; i<=50;i++)
{
DataSet.Tables.DefaultView;
}

--
Mit freundlichen Gruessen - Regards

Ralph Gerbig
(e-mail address removed)
www.ralphgerbig.de.vu

Because of some spam isues I'm not able
to response to all e-mail requests.
 
H

Hardy Wang

Thanks!
But not loop through DataTables.
What I need are records within a DataView from record 25 to record 50.
 
W

William Ryan

If you are using the DefaultView and you don't have a filter condition, then
Ralph's implementation will work. Assuming that you only want to work with
a DataView, you can use the same logic, just change the reference from
DataSet.Table to DataView. In general, I prefer an enumerator like this,
but it's 6 of one half dozen of the other but you can use a for loop for a
better implementation for this specific issue...

for(int i = 25; i<=50; i++){
Debug.WriteLine(row;
}

If you have a column who's number corresponds to row value, then you can use
something like

DataRow[] dRows = ds.Tables[0].Select("ID > 25 and ID < 51) //Since you are
using Defaultview, this should work as well even if you don't use the view
per se.

--This is the enumerated example, just for reference b/c it's the better
solution here.
DataView v = new DataView(ds.Tables[0].DefaultView);

DataRowView dro;

IEnumerator myEnum = v.GetEnumerator();
int x = 0;
while(myEnum.MoveNext()){
//Check for the row position
if(x >25 and x < 51){
Debug.WriteLine(
}
i++
}
 
M

Manoj G [MVP]

You can create a new DataColumn (say RowNum) and set its AutoIncrement
property to true and have its AutoIncrementSeed and AutoIncrementStep as 1.
You can add this column to the Datatable b4 you do a Fill. To keep things
simple, select a column name that does not clash with the result set schema.
Finally, after a DataView is created from the DataTable, you can use a
FilterExpression likeRowNum >= 25 and RowNum <= 50.
I am not sure if a simpler approach is possible.
 
S

Steve

When you fill the DataSet, you can specify the start
record number and end record number, or you could try
using the PagedDataSource if you know that you always
want 25 (or a set amount of) records.

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top