You are not Logged in
Would you like to Login or Register

Today is: Wednesday, 19 November, 2008
Check this months hot topics

Restricting the number of rows in a dataview when binding to a repeater

The problem

I needed to restrict the number of items bound to an asp:repeater. Basically I never wanted more than 10 items to be displayed.

Options

The right thing to do would be to restrict the number of items at the database level using Set RowCount or top 10.

In this instance, it wasn't an option and the lack of a RowCount feature on the DataView object left me needing to find another solution.

The Solution

After some searching, I came across a bit of code which proved helpful. The following code makes allows me to pass a DataView into an enumerable object which returns only the number of rows I'm interested in.

Example Use

myRepeater.DataSource = new FilterRows(myDataView, 10);

The Enumerable Object

using System;
using System.Data;
using System.Web.Security;
using System.Collections;

/// <summary>
/// Summary description for FilterRows
/// </summary>
public class FilterRows : IEnumerable
{
    DataView dataView;
    private int rowsToShow;

    public FilterRows(DataView dataView, int rowsToShow)
    {
        this.rowsToShow = rowsToShow;
        this.dataView = dataView;
    }

    public IEnumerator GetEnumerator()
    {
        return new PageOfData(this.dataView.GetEnumerator(), this.rowsToShow);
    }


    internal class PageOfData : IEnumerator
    {
        private IEnumerator e;
        private int cnt = 0;
        private int rowsToShow;

        internal PageOfData(IEnumerator e, int rowsToShow)
        {
            this.rowsToShow = rowsToShow;
            this.e = e;
        }

        public object Current
        {
            get { return e.Current; }
        }

        public bool MoveNext()
        {
            // If we've hit out limit return false
            if (cnt >= rowsToShow) 
                return false;

            // Track the current row
            cnt++;

            return e.MoveNext();
        }

        public void Reset()
        {
            e.Reset();
            cnt = 0;
        }
    }
}

kick it on DotNetKicks.com del.icio.us digg Mister Wong YahooMyWeb Reddit Furl Spurl blogmarks
Paul Hayman Skype
Author : Paul Hayman
Published : Wednesday, 16 August, 2006

Paul is the COO of kwiboo ltd consultant and has more than a decade of IT consultancy experience. He has consulted for a number of blue chip companies and has been exposed to the folowing sectors: Utilities, Telecommunications, Insurance, Media, Investment Banking, Leisure, Legal, CRM, Pharmaceuticals, Interactive Gaming, Mobile Communications, Online Services. Paul is the COO and co-founder of kwiboo (http://www.kwiboo.com/) and is also the creator of GeekZilla.

Comments

Chris Roberts said:

I like what you've done here Paul. You're showing the first 10 items, but say you had 100 items. How do you show the next 10 and then the next 10 and so on (basically paging)?

March 31, 2007 - 8:38 AM

Akash Dwivedi said:

Good one. you did really good work. I searched for this at many places but got solution at your site. :)

May 16, 2007 - 3:25 PM

Akash Dwivedi said:

Good one. you did really good work. I searched for this at many places but got solution at your site. :)

May 16, 2007 - 3:25 PM

udeeb said:

this really works but I have one question.

Once I get the top 10 from dataview, is there any way that I can change the existing dataview to only contain those 10 records, so that I can sort and change those remaining data only?

Thanks for your help...

July 28, 2008 - 9:36 PM

phayman said:

udeeb,

I'd be inclined to use Generics or Linq to do this now. Check the following article:

http://www.geekzilla.co.uk/View3B286690-73F9-41B2-A882-DDAD91647B7F.htm

Paul

July 29, 2008 - 9:06 AM

Add Comment

Enter your comment below and it will be submitted for moderation.

Your Name

Add Tag

Please enter tags for this article, seperated by semi-colon ;

View Tag's by : # articles | # views