Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > restricting # of rows displayed in a datagrid or datalist

Reply
Thread Tools

restricting # of rows displayed in a datagrid or datalist

 
 
wh1974
Guest
Posts: n/a
 
      08-05-2004
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.


 
Reply With Quote
 
 
 
 
Scott G.
Guest
Posts: n/a
 
      08-05-2004

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:(E-Mail Removed)...
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.
>
>
>

 
Reply With Quote
 
 
 
 
wh1974
Guest
Posts: n/a
 
      08-06-2004
Thanks to both of you for your ideas.

Much appreciated.
Wayne.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Japanese Text not displayed on Image Generated by Servlet on winXP, Linux but displayed on Win2000 boney Java 1 12-15-2006 02:24 PM
Set maximum Rows to be displayed in a datagrid Nicolaj ASP .Net Datagrid Control 4 08-14-2006 02:00 PM
No. of rows displayed in paged datagrid Andrew Chalk ASP .Net Datagrid Control 2 11-12-2005 07:19 PM
Setting up a datalist control - Item_DataBound for a datalist in a datalist Nevyn Twyll ASP .Net 8 09-09-2004 10:13 PM
static rows in datagrid, datalist or repeater Reddy ASP .Net 1 01-15-2004 04:34 PM



Advertisments