How to get " TOP(X)" rows from a DataView?

  • Thread starter Thread starter jensen bredal
  • Start date Start date
J

jensen bredal

I have a dataView and i only want to get the first x rows .

Is there a way of doing this without using Foreach?

Thanks

JB
 
Am I missing something obvious by suggesting that you use an SQL statement
that only gets the rows you need?

Such as "SELECT TOP 10 * FROM musictracks WHERE genre = "Jazz" ORDER BY
artist;"

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Well, i have cached my dataview and i want to any time extract x rows from
it.
X may varie from time to time depending on the user of the apllication.
The problem , is that i want to have it cached so a select wouldn't do,
Or would be prefered on the cached dataview.

Hope that was a bit more clear.
JB
 
By reading the documentatation , it does not look like on can use:

"SELECT TOP 10 * FROM musictracks WHERE genre = "Jazz" ORDER BY

on the RowFilter property.
 
Add a DataColumn to you datatable and set the AutoIncrment value to true.
This will create a sequence for you. Make sure the data is sorted
beforehand in the order that you want. THen you can just use Rowfilter =
"ComputedColumn < 50"
 
Hi,

AFAIK this feature does not exist in the framework, ITOH it's easily
doable, just wrap ( or extend ) DataView , add a botton/top limit and recode
the indexer

The wrap is the easiest thing to do as you probably only need this filtering

Cheers,
 
The wrap is the easiest thing to do as you probably only need this
filtering

Cheers,

Well i would appreciate sample code for your solution as it will extend my
horizons a bit furthere although i find Ryans solution very appropriate for
my need.

Many Thanks
JB
 
Jensen:

Here's a quick mockup of how it'll work:

private void SimulateTop(System.Int32 topValue){

DataTable SampleTable = new DataTable("JensensTable");

//DataAdapter fill code in here.

DataColumn ExpressionColumn = new DataColumn("Indexer",
typeof(System.Int32));

ExpressionColumn.AutoIncrement = true;

ExpressionColumn.AutoIncrementStep = 1;//It defaults to 1 - I just put it
here for clarity

ExpressionColumn.AutoIncrementSeed = 0;//Again, it defaults to this - I just
included it for clarity

DataView SampleView = SampleTable.DefaultView;

SampleView.RowFilter = "Indexer < " + topValue.ToString();

//You may want to include a minValue as well and change the rowfilter so
that you can specify other

//ranges, but by defaulting to 0 - you'll effectively have your Top
Condition

}
 
Great ,Great i did in fact got right. The only things i did not include in
my code was
ExpressionColumn.AutoIncrementStep = 1
and

ExpressionColumn.AutoIncrementSeed =0;

But as you mentioned, if they default to theses values then i guess i'm
happy.

I think i perfectly understood your explaination the first time. But it is
also nice to know that
your code is in fact is what i did.


Again thank you so much


JB
 
Hi Ryan,
there is some problem with the code.

The computed row contains System.DBNull.Value

causing the Rowfilter operation to retunrn DataView.Count=0.

Can you see what i'm missing?

Thanks
 
Back
Top