Filtering a DataView

  • Thread starter Thread starter Cindy Kee
  • Start date Start date
C

Cindy Kee

I want to filter a dataview to include only the first 5 records of the
datatable, or filter records 8-18 in the datatable. How do I write a filter
for that? I want to use this to implement paging in a datalist control. I
am not using a dataadapter because I am getting the dataset from a
webservice, so I have no control over how many records come back.

Anyone?
 
Cindy,

I had to reply since you are a Kee too!

I can think of two methods.

One. Since you have the DS you could add an int column to the
particular DataTable, go thru the Rows Collection and increment the
column value by 1. Now you have a row number and can use RowFilter in
your DataView. (Probably not the most efficient)

Two. Since you are using the DataList you can control what rows are
displayed in the ItemDataBound event. Use the e.Item.ItemIndex.

Jona Kee
 
Cindy,

Another option is to construct a RowFilter that just the
desired rows satisfy. Say you want to display customers sorted
by country, along with the ability to show just a page of rows
(#11 - #20) in the DataView. First, set the DataView's Sort
property to Country plus CustomerID so there's a definite order:

vue.Sort = "Country, CustomerID"

When I look at customer #11 in this view, the country is
Brazil and the CustomerID is HANAR. So, if I want to view
customer #11 and greater, I would set the RowFilter property to:

"Country > 'Brazil' OR (Country = 'Brazil' AND CustomerID >=
'HANAR')"

Customer #20 is in Denmark with a CustomerID of SIMOB. A
RowFilter to view just the customers up to #20 would be:

"Country < 'Denmark' OR (Country = 'Denmark' AND CustomerID <=
'SIMOB')"

Now, you can combine these filters, add parenthesis, and add
calls to the desired rows in the DataView to get code like the
following, and I apologize ahead of time for the inevitable
problem with line breaks.

VB.NET:
Dim rowFirst, rowLast As DataRowView
rowFirst = vue(10)
rowLast = vue(19)
vue.RowFilter = string.Format("(Country > '{0}' OR (Country =
'{0}' AND CustomerID >= '{1}')) AND (Country < '{2}' OR (Country
= '{2}' AND CustomerID <= '{3}'))", rowFirst("Country"),
rowFirst("CustomerID"), rowLast("Country"), rowLast("CustomerID"))

C#:
DataRowView rowFirst, rowLast;
rowFirst = vue[10];
rowLast = vue[19];
vue.RowFilter = string.Format("(Country > '{0}' OR (Country =
'{0}' AND CustomerID >= '{1}')) AND (Country < '{2}' OR (Country
= '{2}' AND CustomerID <= '{3}'))", rowFirst["Country"],
rowFirst["CustomerID"], rowLast["Country"],
rowLast["CustomerID"]);

Not exactly elegant, but quite effective.

I hope this information proves helpful.

David Sceppa
Microsoft
This posting is provided "AS IS" with no warranties,
and confers no rights. You assume all risk for your use.
© 2003 Microsoft Corporation. All rights reserved.
 
Back
Top