By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,935 Members | 1,617 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,935 IT Pros & Developers. It's quick & easy.

Data grid with current page combo box selector

P: n/a

I have a data grid that is set up like this

Page items displayed = 10
EnableViewState = false (i dont want to send large amounts of data over the
CustomPaging = false
AllowPaging = true

now at the bottom of the data grid page, i have the numbers showing up as
the paging method, i got it to switch pages and show the proper page when
enableviewstate = false was on, but what I need to do now is show a combo
box above the grid that says Page x of y is the current one showing then in
the list show all the pages availabe to select... that part I have done... i
based it on the pages count in the grid and the value of the item for the
combo box is the page index of the grid that page is associated with... but
all the attemps at making the newly selected item in the combo box change
the current page in the data grid have failed... i tried changeing the data
grid's curent page index and got nothing out of it... how would you go about
doing this?! thanks
Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies

P: n/a
Hi Brian,

As for the DataGrid paging problem without ViewState Enabled, I think the
most important thing is to rebind the grid with data when there is any
event fired which indicate us to change the grid's page index. For example,
in the datagrid's PageIndexChanged event, do the following things in

grid.CurrentPageIndex = e.NewPageIndex;

And since you use a dropdownlist to let the user select a certain page
index to change the page index, you also need to do the samething in the
dropdownlist's SelectedIndexChange event, such as:

get selected index from the dropdownlist
set it to the datagrid's CurrentPageIndex property
Rebind grid with data

and here is a test page I've made, you can have a test on your side if you
have anything unclear:
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content=""
<form id="Form1" method="post" runat="server">
<table width="100%" align="center">
<td style="HEIGHT: 44px">CurrentPage:<asp:dropdownlist id="lstCurrent"
EnableViewState="True" Runat="server"
Page Count:
<asp:label id="lblPageCount" Runat="server"></asp:label></td>
<td><asp:datagrid id="dgPage" runat="server" EnableViewState="False"
AllowPaging="True" PageSize="5">
<PagerStyle Mode="NumericPages"></PagerStyle>

=========code behind===================
public class PageSelector : System.Web.UI.Page
protected System.Web.UI.WebControls.DropDownList lstCurrent;
protected System.Web.UI.WebControls.Label lblPageCount;
protected System.Web.UI.WebControls.DataGrid dgPage;

private void Page_Load(object sender, System.EventArgs e)
Session["DATASOURCE"] = GetDataSource();


protected DataTable GetDataSource()
DataTable tb = new DataTable();

DataRow dr = null;
bool[] flags = {true,false};
for(int i=1;i<=27;++i)
dr = tb.NewRow();
dr["index"] = i.ToString();
dr["name"] = "Name" + i.ToString();
dr["price"] = 3.434 * (i%3 +1);


return tb;

protected void Bind_Grid()
DataTable tb =(DataTable)Session["DATASOURCE"];
dgPage.DataSource = tb;

for(int i=1;i<=dgPage.PageCount;++i)
lblPageCount.Text = dgPage.PageCount.ToString();


#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
// CODEGEN: This call is required by the ASP.NET Web Form Designer.

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
this.lstCurrent.SelectedIndexChanged += new
System.EventHandler(this.lstCurrent_SelectedIndexC hanged);
this.dgPage.PageIndexChanged += new
System.Web.UI.WebControls.DataGridPageChangedEvent Handler(this.dgPage_PageIn
this.Load += new System.EventHandler(this.Page_Load);


private void dgPage_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEvent Args e)
dgPage.CurrentPageIndex = e.NewPageIndex;
lstCurrent.SelectedIndex = e.NewPageIndex;


private void lstCurrent_SelectedIndexChanged(object sender,
System.EventArgs e)
int index = lstCurrent.SelectedIndex;

dgPage.CurrentPageIndex = index;

================================================== ===

Also, if you have any other quetsions, please feel free to post here.


Steven Cheng
Microsoft Online Support

Get Secure!
(This posting is provided "AS IS", with no warranties, and confers no

Get Preview at ASP.NET whidbey

Nov 18 '05 #2

P: n/a
Hi Brian,

Have you had a chance to check out the suggestions in my last reply or have
you got any further ideas on this issue? If you have anything unclear or if
there're anything else we can help, please feel free to post here. Thanks.


Steven Cheng
Microsoft Online Support

Get Secure!
(This posting is provided "AS IS", with no warranties, and confers no

Get Preview at ASP.NET whidbey

Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.