How to do more advanced paging with the DataGrid control

D

Daniel Walzenbach

Hi,



I have a question regarding the DataGrid control. If paging is enabled the
grid binds the data, sets the paging on the top/bottom (or however it is set
up) and throws away unnecessary data. So far so good for a tiny amount of
data but if some 100000 of rows are the source this approach is stupid.



I therefore want to supply the grid with only the data it needs but then are
faced with the following problem. How can the grid be set up that the paging
UI (1.4 5 6.10) is displayed correctly? If the source e.g. is 1000 rows and
paging 20 rows the user would expect the paging UI to display 1 - 50. Since
the grid only is bound to 20 rows now paging UI will be available.



Does anybody know how to overcome this limitation? I thought about creating
my own version of the DataGrid control but don't know how to start? How can
I figure out how the DataGrid works to get an idea of what is to change?



Thank you a lot in advance!



Daniel
 
P

Peter Styk

I use this code

private PagedDataSource DoPaging(Contents contents)

{

pagedData.DataSource = contents;

pagedData.AllowPaging = true;


try

{

pagedData.CurrentPageIndex =
Int32.Parse(Request.QueryString[pageNoParam].ToString());

}

catch

{

pagedData.CurrentPageIndex = 0;

}

btnPrev.Visible = !pagedData.IsFirstPage;

btnNext.Visible = !pagedData.IsLastPage;

pages.Text = @"<p align=""center""><b>&nbsp; </b>PAGE:";

for (int x = 1; x <= pagedData.PageCount; x++)

{

// if current page then

if (pagedData.CurrentPageIndex + 1 == x)

{

pages.Text += " | ";

pages.Text += "<b>" + (pagedData.CurrentPageIndex + 1).ToString() + "</b>";

}

else

{

pages.Text += " | ";

string oldQueryString = Util.GetQueryStringText(Request.QueryString,
pageNoParam);

pages.Text += "<a href=" + oldQueryString + "&" + pageNoParam + "=" + (x -
1).ToString() + ">";

pages.Text += x + "</a>";

}

}

pages.Text += @" | </p>";


return pagedData;

}

sorry no indents, it shortens your dataset, that;s what PagedDataSource
does. You will need two buttons for left and right and each will need pass
parameters (pageNumber) to next page so the object can figure out the offset



private void btnPrev_Click(object sender, System.Web.UI.ImageClickEventArgs
e)

{

string oldQueryString = Util.GetQueryStringText(Request.QueryString,
pageNoParam);

Response.Redirect(oldQueryString + "&" + pageNoParam + "=" +
(pagedData.CurrentPageIndex - 1));

}

private void btnNext_Click(object sender, System.Web.UI.ImageClickEventArgs
e)

{

string oldQueryString = Util.GetQueryStringText(Request.QueryString,
pageNoParam);

Response.Redirect(oldQueryString + "&" + pageNoParam + "=" +
(pagedData.CurrentPageIndex + 1));

}





now all you need is to drop your current dataset and execute it



pagedData.PageSize = gridSize;

dlList.RepeatColumns = gridColumns;

dlList.DataSource = DoPaging(contents);

dlList.DataBind();







define you paged data at the top of your class




protected PagedDataSource pagedData = new PagedDataSource();







her ya go



Peter
 
D

Daniel Walzenbach

Peter,

thanks for the tip.

Daniel

Peter Styk said:
I use this code

private PagedDataSource DoPaging(Contents contents)

{

pagedData.DataSource = contents;

pagedData.AllowPaging = true;


try

{

pagedData.CurrentPageIndex =
Int32.Parse(Request.QueryString[pageNoParam].ToString());

}

catch

{

pagedData.CurrentPageIndex = 0;

}

btnPrev.Visible = !pagedData.IsFirstPage;

btnNext.Visible = !pagedData.IsLastPage;

pages.Text = @"<p align=""center""><b>&nbsp; </b>PAGE:";

for (int x = 1; x <= pagedData.PageCount; x++)

{

// if current page then

if (pagedData.CurrentPageIndex + 1 == x)

{

pages.Text += " | ";

pages.Text += "<b>" + (pagedData.CurrentPageIndex + 1).ToString() +
"</b>";

}

else

{

pages.Text += " | ";

string oldQueryString = Util.GetQueryStringText(Request.QueryString,
pageNoParam);

pages.Text += "<a href=" + oldQueryString + "&" + pageNoParam + "=" + (x -
1).ToString() + ">";

pages.Text += x + "</a>";

}

}

pages.Text += @" | </p>";


return pagedData;

}

sorry no indents, it shortens your dataset, that;s what PagedDataSource
does. You will need two buttons for left and right and each will need
pass
parameters (pageNumber) to next page so the object can figure out the
offset



private void btnPrev_Click(object sender,
System.Web.UI.ImageClickEventArgs
e)

{

string oldQueryString = Util.GetQueryStringText(Request.QueryString,
pageNoParam);

Response.Redirect(oldQueryString + "&" + pageNoParam + "=" +
(pagedData.CurrentPageIndex - 1));

}

private void btnNext_Click(object sender,
System.Web.UI.ImageClickEventArgs
e)

{

string oldQueryString = Util.GetQueryStringText(Request.QueryString,
pageNoParam);

Response.Redirect(oldQueryString + "&" + pageNoParam + "=" +
(pagedData.CurrentPageIndex + 1));

}





now all you need is to drop your current dataset and execute it



pagedData.PageSize = gridSize;

dlList.RepeatColumns = gridColumns;

dlList.DataSource = DoPaging(contents);

dlList.DataBind();







define you paged data at the top of your class




protected PagedDataSource pagedData = new PagedDataSource();







her ya go



Peter











Daniel Walzenbach said:
Hi,



I have a question regarding the DataGrid control. If paging is enabled
the
grid binds the data, sets the paging on the top/bottom (or however it is set
up) and throws away unnecessary data. So far so good for a tiny amount of
data but if some 100000 of rows are the source this approach is stupid.



I therefore want to supply the grid with only the data it needs but then are
faced with the following problem. How can the grid be set up that the paging
UI (1.4 5 6.10) is displayed correctly? If the source e.g. is 1000 rows and
paging 20 rows the user would expect the paging UI to display 1 - 50. Since
the grid only is bound to 20 rows now paging UI will be available.



Does anybody know how to overcome this limitation? I thought about creating
my own version of the DataGrid control but don't know how to start? How can
I figure out how the DataGrid works to get an idea of what is to change?



Thank you a lot in advance!



Daniel
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top