restricting # of rows displayed in a datagrid or datalist

W

wh1974

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.
 
S

Scott G.

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 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()
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top