Link to home
Start Free TrialLog in
Avatar of MCofer
MCofer

asked on

How to get TOP 10 rows of a DataView

I want to filter a dataview to match the results of SELECT TOP 10
I am calling this from asp codebehind and do not want to make another trip to the database
The Row to filter is named "PartNum"
myDataView.RowFilter = "TOP 10 PartNum"
Avatar of igor_alpha
igor_alpha

Hi MCofer,

You can't specify DataView RowFilter such as "TOP 10 PartNum".
It because RowFilter accept the name of a column followed by an operator and a value to filter on.
For example "LastName = 'Smith'"

But you can retrive top records from DataSet.Table and then bind they to datagrid.
Look at following function returning top rows:


Function GetTopRecords(ByVal dt, ByVal RowCount, ByVal _Start)
        Dim _table As DataTable
        _table = dt.Clone()
        Dim i As Int32
        For i = _Start To _Start + RowCount
            If i >= dt.Rows.Count Then
                Exit For
            Else
                _table.ImportRow(dt.Rows(i))
            End If
        Next

        Return _table
    End Function
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MCofer

ASKER

That was easy enough (its DataRowView not DataViewRow)

Thanks
That's what happens when you write code in the comment block.  No syntax checking!!!!

Bob
'For what it is worth, another simple solution:

Dim ds As DataSet = SomePropertyOrMethodToGetMyData
'create increment column for ordering
If Not ds.Tables(0).Columns.Contains("TopOrder") Then
  Dim cOrder As New DataColumn("TopOrder", GetType(Integer))
  cOrder.AutoIncrement = True
  ds.Tables(0).Columns.Add(cOrder)
End If
'filter data
Dim dv As New DataView(ds.Tables(0))
dv.RowFilter = "TopOrder <=" & MaxRecords.ToString

'Access your dataview for the filtered records