restricting # of rows displayed in a datagrid or datalist

Discussion in 'ASP .Net' started by wh1974, Aug 5, 2004.

  1. wh1974

    wh1974 Guest

    I have a DataTable that I'm binding to a DataList control on my web page. I
    want to be able to restrict the number of rows that are initially displayed
    from the DataTable. For example I want to be able to show the first 5 rows,
    a button will then allow the user to 'show all records'.

    Although I've used a DataView before, I am unsure how this can be of use
    when I need to restrict the number of rows. I am aware of the RowFilter
    property, but this only allows me to specify a SQL like filter.

    Thanks,
    Wayne.
    wh1974, Aug 5, 2004
    #1
    1. Advertising

  2. wh1974

    Scott G. Guest

    I think you can be a bit more clever than copying the rows into another table; I would try creating a small helper class that implements IEnumerable and wraps a DataView; just off the top of my head, here something that might get you started:

    MyDataList.DataSource = new Show5(MyView);


    public class Show5 : IEnumerable
    {
    DataView m_view;
    public Show5(DataView v)
    {
    m_view = v;
    }

    public IEnumerator GetEnumerator()
    {
    return new E5(m_view.GetEnumerator());
    }

    internal class E5 : IEnumerator {
    private IEnumerator m_e;
    private int m_cnt = 0;
    internal E5(IEnumerator e) {
    m_e = e;
    }

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

    public bool MoveNext() {
    if (m_cnt > 5) return false;
    m_cnt++;
    return m_e.MoveNext();
    }

    public void Reset() {
    m_e.Reset();
    m_cnt = 0;
    }
    }
    }

    "Greg Hurlman" <ghurlman*AT*squaretwo*DOT*net> wrote in message news:...
    To do this, you'll want to use the DataTable's DefaultView to set any sorting
    or filtering you have, and then create a new DataTable, clone the old
    datatable, and then import the first 5 rows from the original DataTable's
    dataview, like this:

    Dim OriginalView As DataView = OriginalTable.DefaultView
    OriginalView.RowFilter = "Whatever"
    OriginalView.Sort = "Whatever"

    Dim NewTable As DataTable = OriginalTable.Clone()
    For i As Integer = 1 To 5
    NewTable.ImportRow(OriginalView(i).Row)
    Next

    MyDataGrid.DataSource = NewTable
    MyDataGrid.DataBind()

    "wh1974" wrote:

    > I have a DataTable that I'm binding to a DataList control on my web page. I
    > want to be able to restrict the number of rows that are initially displayed
    > from the DataTable. For example I want to be able to show the first 5 rows,
    > a button will then allow the user to 'show all records'.
    >
    > Although I've used a DataView before, I am unsure how this can be of use
    > when I need to restrict the number of rows. I am aware of the RowFilter
    > property, but this only allows me to specify a SQL like filter.
    >
    > Thanks,
    > Wayne.
    >
    >
    >
    Scott G., Aug 5, 2004
    #2
    1. Advertising

  3. wh1974

    wh1974 Guest

    Thanks to both of you for your ideas.

    Much appreciated.
    Wayne.
    wh1974, Aug 6, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Reddy
    Replies:
    1
    Views:
    699
    Curt_C [MVP]
    Jan 15, 2004
  2. Nevyn Twyll
    Replies:
    8
    Views:
    1,867
  3. boney
    Replies:
    1
    Views:
    563
  4. Andrew Chalk

    No. of rows displayed in paged datagrid

    Andrew Chalk, Nov 12, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    122
    Andrew Chalk
    Nov 12, 2005
  5. Nicolaj

    Set maximum Rows to be displayed in a datagrid

    Nicolaj, Aug 13, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    4
    Views:
    213
Loading...

Share This Page