DataGrid paging

G

Guest

Hello everybody, I got myself a strange problem

I have a DataGrid in the page, nothing special about it, only it connects datasource programatically and it supports paging.y. So the code is something like this

private void Page_Load(object sender, System.EventArgs e

if (!IsPostBack
GridBind()

private void GridBind(

myGrid..DataSource = GetSomeDataSource()
myGrid.DataBind()

private void PageChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e

myGrid.CurrentPageIndex = e.NewPageIndex
BindData()


This worked perfectly OK. Then I decided to try also to create grid columns dynamically. So I set the AutoGenerateColumns property to false. I changed the code, so it would create some columns like this

private void Page_Load(object sender, System.EventArgs e

AddSomeColumnsToGrid();

if (!IsPostBack
GridBind()


Now when I load the page for the 1st time, everything is OK. When I want to go to another page grid mystically disappears. I tried to debug the page setting breakpoint inside the PageChanged event and I never got there
Being desperate I tried everything possible then I came to something like this

private void Page_Load(object sender, System.EventArgs e

AddSomeColumnsToGrid();

//if (!IsPostBack
GridBind()


and it worked. But now I am creating dataset 2 times in postback (supposing it's the page changed event)

Am I doing anything wrong? Because I really don't know how to solve this problem. Any helps would be very appreciated.
 
G

Guest

I made some errors in the first source code, it should be this

private void Page_Load(object sender, System.EventArgs e

if (!IsPostBack
GridBind()

private void GridBind(

myGrid.DataSource = GetSomeDataSource()
myGrid.DataBind()

private void PageChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e

myGrid.CurrentPageIndex = e.NewPageIndex
GridBind()
 
G

Guest

Add the AddSomeColumnsToGrid() method to the GridBind method. That way, any time you are going to bind the grid you know you have the columns.
 
G

Guest

I tried what you suggested, but the problem stays. Let's revisit the code once more (as you proposed):

private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
GridBind();
}
private void GridBind()
{
AddSomeColumnsToGrid();

myGrid.DataSource = GetSomeDataSource();
myGrid.DataBind();
}
private void PageChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
myGrid.CurrentPageIndex = e.NewPageIndex;
GridBind();
}

Again it works OK when the page is loading for the 1st time. When I try to get another page, grid is gone. Debugging the project I found out that the execution never reaches the PageChanged handler. That is what confuses me mostly.

Because when I comment the line in Page_load like this:

private void Page_Load(object sender, System.EventArgs e)
{
// if (!IsPostBack)
GridBind();
}

it works. But now, unfortunately, the grid shows 2 copies of columns, logically, because I added them twice.

Resolution - the biggest problem as I see it is, that the execution is not passed to the handler of PageChanged event when postback occures, when I hit page number at the bottom of the grid.

This is pretty weird, I know. But that is why the solution is not easy to find in ANY help. Thanks in advance for further suggestions.
 

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,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top