using DataViews to jump to a sorted set of records without scanning the whole view

  • Thread starter Thread starter J. C. Clay
  • Start date Start date
J

J. C. Clay

I've created a DataView in C#.

I've also sorted my records on product_name.

If I return 6000 products and want to get to products
(3000-3020) I wrote the following code:

WriteView(myView,3000,3020)

public void WriteView(DataView myView,int startIdx,int endIdx)
{
int n;
n=0;
myView.Sort="product_name";

foreach (DataRowView myDRV in myView)

{
n=n+1;
for (int i = 0; i < myView.Table.Columns.Count; i++)
{
if ((n > startIdx) && (n < endIdx))
{
Console.Write(myDRV + "\t");
Console.WriteLine("");
}
}
}

When I run this it works but I wonder if I can get the same result
without having to scan through the DataView.

I've tried skipping over the first 3000 records using the command
Console.WriteLine(myView.Table.Rows[j]);

where j=3000 to 3020 but this does not return the sorted order; it
returns the DataView in the intial sequence that it was first fetched
from the dbase.

Does anyone have any other suggestions.

Thanks

J.C. Klay
 
J. C. Clay,

If the index is stored in the data set itself in a column, then you can
set the RowFilter property to the following and it should work:

// Set the filter to only include items in the index range.
myView.RowFilter = String.Format("<fieldname> > {0} and <fieldname> < {1}",
startIdx, endIdx);

Hope this helps.
 
Back
Top