How to get the TOP X rows from a DataTable or DataView#

Here is a function I found that can trim down a datatable/dataview to a limited number of rows.  Basically, it's like doing a "TOP X".

''' <summary>
''' Will return a dataview with only the top "number" of rows.
''' </summary>
''' <param name="myDataView">dataview to "trim"</param>
''' <param name="number">the number of rows to return</param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function GetTopDataViewRows(ByVal myDataView As DataView, ByVal number As Integer) As DataView
    Dim tableToClone As DataTable = myDataView.Table.Clone
    Dim i As Integer
    For i = 0 To number - 1
        If i >= myDataView.Count Then Exit For
        tableToClone.ImportRow(myDataView(i).Row)
    Next
    Return New DataView(tableToClone, myDataView.RowFilter, myDataView.Sort, myDataView.RowStateFilter)
End Function

I didn't write this, credit goes to this post.

Categories:  | 
Friday, February 23, 2007 3:06:17 PM (Central Standard Time, UTC-06:00) #    Comments [0]  | 

 

Name
E-mail
(will show your gravatar icon)
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

All content © 2008, Christopher May, Inc
Open Job Positions
On this page
Google Ads
This site
Calendar
<December 2008>
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910
Archives
Sitemap
Blogroll OPML
Disclaimer

Powered by: newtelligence dasBlog 1.9.6264.0

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Send mail to the author(s) E-mail

Theme design by Jelle Druyts


Pick a theme: