Paging problems

R

Ruy Castelli

Hello,

I'm learning to code in C# and I created a DataGrid component, which I
couldn't get it to work properly either using "Next >>" and "<< Previous"
buttons or using actual page numbers.

When I use the "Next" and "Previous" buttons, I move on one page (going from
page 1 to 2). Then if I click next on page 2, it comes back to page 2. I
checked my code several times and I can't find anything wrong with it. If I
click on Previous, it comes back to page 1.

When I use the actual page numbers, on the very first time the applications
shows all possible page numbers possible (1 to 10 maybe). Then if I click on
page 2, it refreshes and shows the correct number of pages (4) and only
links 1 to 4 are displayed. Now, I can access any page I want and the
application displays it properly, the problem is on the first time it
displays the data.

Basically, it's a page when 2 TextBoxes and one Button. When the user clicks
on this button, the application adds data to a database and displays all
rows.

I'm not using Page_Load for that, let me know if I'm wrong. I'm just using
the Button_Click event:

private void Button1_Click(object sender, System.EventArgs e)
{
try
{
oleDbConnection1.Open();
if (name.Text.Trim().Length>0 &&
msg.Text.Trim().Length>0)
{
oleDbDataAdapter1.InsertCommand.CommandText =
"INSERT INTO [msg log] "+
"(hour, name, msg) "+
"VALUES ("+
"'"+DateTime.Now.ToString()+"', "+
"'"+name.Text.Trim()+"', "+
"'"+msg.Text.Trim()+"')";
oleDbDataAdapter1.InsertCommand.ExecuteNonQuery();
name.Text = "";
msg.Text = "";
}
oleDbDataAdapter1.SelectCommand.CommandText =
"SELECT hora, nome, msg FROM [msg log]";
oleDbDataAdapter1.Fill(dataset11);
oleDbConnection1.Close();
DataGrid1.DataBind();
}
catch (System.Data.OleDb.OleDbException exception)
{
Error.Visible = true;
Error.Text = "Error on server: " + exception.ToString();
}
}

And here is my PageIndexChanged Event:

private void DataGrid1_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
}

Please, let me know if there is something very wrong with it.

Thanks.
Ruy.
 
D

Dave

private void DataGrid1_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
}


Where is your DataSource??


--
Dave Sexton
[email protected]
-----------------------------------------------------------------------
Ruy Castelli said:
Hello,

I'm learning to code in C# and I created a DataGrid component, which I couldn't get it to work properly either using "Next >>" and
"<< Previous" buttons or using actual page numbers.

When I use the "Next" and "Previous" buttons, I move on one page (going from page 1 to 2). Then if I click next on page 2, it
comes back to page 2. I checked my code several times and I can't find anything wrong with it. If I click on Previous, it comes
back to page 1.

When I use the actual page numbers, on the very first time the applications shows all possible page numbers possible (1 to 10
maybe). Then if I click on page 2, it refreshes and shows the correct number of pages (4) and only links 1 to 4 are displayed.
Now, I can access any page I want and the application displays it properly, the problem is on the first time it displays the data.

Basically, it's a page when 2 TextBoxes and one Button. When the user clicks on this button, the application adds data to a
database and displays all rows.

I'm not using Page_Load for that, let me know if I'm wrong. I'm just using the Button_Click event:

private void Button1_Click(object sender, System.EventArgs e)
{
try
{
oleDbConnection1.Open();
if (name.Text.Trim().Length>0 &&
msg.Text.Trim().Length>0)
{
oleDbDataAdapter1.InsertCommand.CommandText =
"INSERT INTO [msg log] "+
"(hour, name, msg) "+
"VALUES ("+
"'"+DateTime.Now.ToString()+"', "+
"'"+name.Text.Trim()+"', "+
"'"+msg.Text.Trim()+"')";
oleDbDataAdapter1.InsertCommand.ExecuteNonQuery();
name.Text = "";
msg.Text = "";
}
oleDbDataAdapter1.SelectCommand.CommandText =
"SELECT hora, nome, msg FROM [msg log]";
oleDbDataAdapter1.Fill(dataset11);
oleDbConnection1.Close();
DataGrid1.DataBind();
}
catch (System.Data.OleDb.OleDbException exception)
{
Error.Visible = true;
Error.Text = "Error on server: " + exception.ToString();
}
}

And here is my PageIndexChanged Event:

private void DataGrid1_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
}

Please, let me know if there is something very wrong with it.

Thanks.
Ruy.
 
R

Ruy Castelli

Hello Dave, thanks for your answer.

Now, I'm binding data on the Page_Loads and PageIndexChange events, but the
problem is worse, when I click on Next, the datagrid disapears.

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (!PostBack)
{
try
{
oleDbConnection1.Open();

oleDbDataAdapter1.SelectCommand.CommandText =
"SELECT hour, name, msg FROM [msg log]";

oleDbDataAdapter1.Fill(dataset11);
DataGrid1.DataBind();
}
catch (System.Data.OleDb.OleDbException exception)
{
Error.Visible = true;
Error.Text = "Server error: " + exception.ToString();
}
finally
{
oleDbConnection1.Close();
}
}
}

Here is my PageIndexChanged event:

private void DataGrid1_PageIndexChanged(
object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;

try
{
oleDbConnection1.Open();

oleDbDataAdapter1.SelectCommand.CommandText =
"SELECT hour, name, msg FROM [msg log]";

oleDbDataAdapter1.Fill(dataset11);
DataGrid1.DataBind();
}
catch (System.Data.OleDb.OleDbException exception)
{
Error.Visible = true;
Error.Text = "Server error: " + exception.ToString();
}
finally
{
oleDbConnection1.Close();
}

DataGrid1.DataBind();
}


Dave said:
private void DataGrid1_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
}


Where is your DataSource??


--
Dave Sexton
[email protected]
-----------------------------------------------------------------------
Ruy Castelli said:
Hello,

I'm learning to code in C# and I created a DataGrid component, which I
couldn't get it to work properly either using "Next >>" and "<< Previous"
buttons or using actual page numbers.

When I use the "Next" and "Previous" buttons, I move on one page (going
from page 1 to 2). Then if I click next on page 2, it comes back to page
2. I checked my code several times and I can't find anything wrong with
it. If I click on Previous, it comes back to page 1.

When I use the actual page numbers, on the very first time the
applications shows all possible page numbers possible (1 to 10 maybe).
Then if I click on page 2, it refreshes and shows the correct number of
pages (4) and only links 1 to 4 are displayed. Now, I can access any page
I want and the application displays it properly, the problem is on the
first time it displays the data.

Basically, it's a page when 2 TextBoxes and one Button. When the user
clicks on this button, the application adds data to a database and
displays all rows.

I'm not using Page_Load for that, let me know if I'm wrong. I'm just
using the Button_Click event:

private void Button1_Click(object sender, System.EventArgs e)
{
try
{
oleDbConnection1.Open();
if (name.Text.Trim().Length>0 &&
msg.Text.Trim().Length>0)
{
oleDbDataAdapter1.InsertCommand.CommandText =
"INSERT INTO [msg log] "+
"(hour, name, msg) "+
"VALUES ("+
"'"+DateTime.Now.ToString()+"', "+
"'"+name.Text.Trim()+"', "+
"'"+msg.Text.Trim()+"')";
oleDbDataAdapter1.InsertCommand.ExecuteNonQuery();
name.Text = "";
msg.Text = "";
}
oleDbDataAdapter1.SelectCommand.CommandText =
"SELECT hora, nome, msg FROM [msg log]";
oleDbDataAdapter1.Fill(dataset11);
oleDbConnection1.Close();
DataGrid1.DataBind();
}
catch (System.Data.OleDb.OleDbException exception)
{
Error.Visible = true;
Error.Text = "Error on server: " + exception.ToString();
}
}

And here is my PageIndexChanged Event:

private void DataGrid1_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
}

Please, let me know if there is something very wrong with it.

Thanks.
Ruy.
 
D

Dave

Only bind onload if it's not a postback:

if (!IsPostBack)
{
// Todo: bind the grid
}
else
{
// we don't rebind here because the datagrid will "remember" it's contents upon postbacks using the ViewState.
// This is why the ViewState can be quite large on pages that contain datagrid controls filled with data.
}

--
Dave Sexton
[email protected]
-----------------------------------------------------------------------
Ruy Castelli said:
Hello Dave, thanks for your answer.

Now, I'm binding data on the Page_Loads and PageIndexChange events, but the problem is worse, when I click on Next, the datagrid
disapears.

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (!PostBack)
{
try
{
oleDbConnection1.Open();

oleDbDataAdapter1.SelectCommand.CommandText =
"SELECT hour, name, msg FROM [msg log]";

oleDbDataAdapter1.Fill(dataset11);
DataGrid1.DataBind();
}
catch (System.Data.OleDb.OleDbException exception)
{
Error.Visible = true;
Error.Text = "Server error: " + exception.ToString();
}
finally
{
oleDbConnection1.Close();
}
}
}

Here is my PageIndexChanged event:

private void DataGrid1_PageIndexChanged(
object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;

try
{
oleDbConnection1.Open();

oleDbDataAdapter1.SelectCommand.CommandText =
"SELECT hour, name, msg FROM [msg log]";

oleDbDataAdapter1.Fill(dataset11);
DataGrid1.DataBind();
}
catch (System.Data.OleDb.OleDbException exception)
{
Error.Visible = true;
Error.Text = "Server error: " + exception.ToString();
}
finally
{
oleDbConnection1.Close();
}

DataGrid1.DataBind();
}


Dave said:
private void DataGrid1_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
}


Where is your DataSource??


--
Dave Sexton
[email protected]
-----------------------------------------------------------------------
Ruy Castelli said:
Hello,

I'm learning to code in C# and I created a DataGrid component, which I couldn't get it to work properly either using "Next >>"
and "<< Previous" buttons or using actual page numbers.

When I use the "Next" and "Previous" buttons, I move on one page (going from page 1 to 2). Then if I click next on page 2, it
comes back to page 2. I checked my code several times and I can't find anything wrong with it. If I click on Previous, it comes
back to page 1.

When I use the actual page numbers, on the very first time the applications shows all possible page numbers possible (1 to 10
maybe). Then if I click on page 2, it refreshes and shows the correct number of pages (4) and only links 1 to 4 are displayed.
Now, I can access any page I want and the application displays it properly, the problem is on the first time it displays the
data.

Basically, it's a page when 2 TextBoxes and one Button. When the user clicks on this button, the application adds data to a
database and displays all rows.

I'm not using Page_Load for that, let me know if I'm wrong. I'm just using the Button_Click event:

private void Button1_Click(object sender, System.EventArgs e)
{
try
{
oleDbConnection1.Open();
if (name.Text.Trim().Length>0 &&
msg.Text.Trim().Length>0)
{
oleDbDataAdapter1.InsertCommand.CommandText =
"INSERT INTO [msg log] "+
"(hour, name, msg) "+
"VALUES ("+
"'"+DateTime.Now.ToString()+"', "+
"'"+name.Text.Trim()+"', "+
"'"+msg.Text.Trim()+"')";
oleDbDataAdapter1.InsertCommand.ExecuteNonQuery();
name.Text = "";
msg.Text = "";
}
oleDbDataAdapter1.SelectCommand.CommandText =
"SELECT hora, nome, msg FROM [msg log]";
oleDbDataAdapter1.Fill(dataset11);
oleDbConnection1.Close();
DataGrid1.DataBind();
}
catch (System.Data.OleDb.OleDbException exception)
{
Error.Visible = true;
Error.Text = "Error on server: " + exception.ToString();
}
}

And here is my PageIndexChanged Event:

private void DataGrid1_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
}

Please, let me know if there is something very wrong with it.

Thanks.
Ruy.
 
D

Dave

I noticed that your are binding twice in the DataGrid1_PageIndexChanged event handler. Remove the last line and it should work
fine.

My last post was submitted because I searched for IsPostBack, and didn't find it... I assume this is a type-o?
if (!PostBack)

Should be:

if (!IsPostBack)

--
Dave Sexton
[email protected]
-----------------------------------------------------------------------
Ruy Castelli said:
Hello Dave, thanks for your answer.

Now, I'm binding data on the Page_Loads and PageIndexChange events, but the problem is worse, when I click on Next, the datagrid
disapears.

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (!PostBack)
{
try
{
oleDbConnection1.Open();

oleDbDataAdapter1.SelectCommand.CommandText =
"SELECT hour, name, msg FROM [msg log]";

oleDbDataAdapter1.Fill(dataset11);
DataGrid1.DataBind();
}
catch (System.Data.OleDb.OleDbException exception)
{
Error.Visible = true;
Error.Text = "Server error: " + exception.ToString();
}
finally
{
oleDbConnection1.Close();
}
}
}

Here is my PageIndexChanged event:

private void DataGrid1_PageIndexChanged(
object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;

try
{
oleDbConnection1.Open();

oleDbDataAdapter1.SelectCommand.CommandText =
"SELECT hour, name, msg FROM [msg log]";

oleDbDataAdapter1.Fill(dataset11);
DataGrid1.DataBind();
}
catch (System.Data.OleDb.OleDbException exception)
{
Error.Visible = true;
Error.Text = "Server error: " + exception.ToString();
}
finally
{
oleDbConnection1.Close();
}

DataGrid1.DataBind();
}


Dave said:
private void DataGrid1_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
}


Where is your DataSource??


--
Dave Sexton
[email protected]
-----------------------------------------------------------------------
Ruy Castelli said:
Hello,

I'm learning to code in C# and I created a DataGrid component, which I couldn't get it to work properly either using "Next >>"
and "<< Previous" buttons or using actual page numbers.

When I use the "Next" and "Previous" buttons, I move on one page (going from page 1 to 2). Then if I click next on page 2, it
comes back to page 2. I checked my code several times and I can't find anything wrong with it. If I click on Previous, it comes
back to page 1.

When I use the actual page numbers, on the very first time the applications shows all possible page numbers possible (1 to 10
maybe). Then if I click on page 2, it refreshes and shows the correct number of pages (4) and only links 1 to 4 are displayed.
Now, I can access any page I want and the application displays it properly, the problem is on the first time it displays the
data.

Basically, it's a page when 2 TextBoxes and one Button. When the user clicks on this button, the application adds data to a
database and displays all rows.

I'm not using Page_Load for that, let me know if I'm wrong. I'm just using the Button_Click event:

private void Button1_Click(object sender, System.EventArgs e)
{
try
{
oleDbConnection1.Open();
if (name.Text.Trim().Length>0 &&
msg.Text.Trim().Length>0)
{
oleDbDataAdapter1.InsertCommand.CommandText =
"INSERT INTO [msg log] "+
"(hour, name, msg) "+
"VALUES ("+
"'"+DateTime.Now.ToString()+"', "+
"'"+name.Text.Trim()+"', "+
"'"+msg.Text.Trim()+"')";
oleDbDataAdapter1.InsertCommand.ExecuteNonQuery();
name.Text = "";
msg.Text = "";
}
oleDbDataAdapter1.SelectCommand.CommandText =
"SELECT hora, nome, msg FROM [msg log]";
oleDbDataAdapter1.Fill(dataset11);
oleDbConnection1.Close();
DataGrid1.DataBind();
}
catch (System.Data.OleDb.OleDbException exception)
{
Error.Visible = true;
Error.Text = "Error on server: " + exception.ToString();
}
}

And here is my PageIndexChanged Event:

private void DataGrid1_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
}

Please, let me know if there is something very wrong with it.

Thanks.
Ruy.
 
R

Ruy Castelli

Thanks Dave,

I followed your tips, but it still didn't work. Then I was checking some
configuration and I noticed that somehow the document properties
"enableSessionState" and "enableViewState" were set to false. When I set
then to true, everything worked.

Thanks.
Ruy.

Dave said:
I noticed that your are binding twice in the DataGrid1_PageIndexChanged
event handler. Remove the last line and it should work fine.

My last post was submitted because I searched for IsPostBack, and didn't
find it... I assume this is a type-o?
if (!PostBack)

Should be:

if (!IsPostBack)

--
Dave Sexton
[email protected]
-----------------------------------------------------------------------
Ruy Castelli said:
Hello Dave, thanks for your answer.

Now, I'm binding data on the Page_Loads and PageIndexChange events, but
the problem is worse, when I click on Next, the datagrid disapears.

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (!PostBack)
{
try
{
oleDbConnection1.Open();

oleDbDataAdapter1.SelectCommand.CommandText =
"SELECT hour, name, msg FROM [msg log]";

oleDbDataAdapter1.Fill(dataset11);
DataGrid1.DataBind();
}
catch (System.Data.OleDb.OleDbException exception)
{
Error.Visible = true;
Error.Text = "Server error: " + exception.ToString();
}
finally
{
oleDbConnection1.Close();
}
}
}

Here is my PageIndexChanged event:

private void DataGrid1_PageIndexChanged(
object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;

try
{
oleDbConnection1.Open();

oleDbDataAdapter1.SelectCommand.CommandText =
"SELECT hour, name, msg FROM [msg log]";

oleDbDataAdapter1.Fill(dataset11);
DataGrid1.DataBind();
}
catch (System.Data.OleDb.OleDbException exception)
{
Error.Visible = true;
Error.Text = "Server error: " + exception.ToString();
}
finally
{
oleDbConnection1.Close();
}

DataGrid1.DataBind();
}


Dave said:
private void DataGrid1_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
}


Where is your DataSource??


--
Dave Sexton
[email protected]
-----------------------------------------------------------------------
Hello,

I'm learning to code in C# and I created a DataGrid component, which I
couldn't get it to work properly either using "Next >>" and "<<
Previous" buttons or using actual page numbers.

When I use the "Next" and "Previous" buttons, I move on one page (going
from page 1 to 2). Then if I click next on page 2, it comes back to
page 2. I checked my code several times and I can't find anything wrong
with it. If I click on Previous, it comes back to page 1.

When I use the actual page numbers, on the very first time the
applications shows all possible page numbers possible (1 to 10 maybe).
Then if I click on page 2, it refreshes and shows the correct number of
pages (4) and only links 1 to 4 are displayed. Now, I can access any
page I want and the application displays it properly, the problem is on
the first time it displays the data.

Basically, it's a page when 2 TextBoxes and one Button. When the user
clicks on this button, the application adds data to a database and
displays all rows.

I'm not using Page_Load for that, let me know if I'm wrong. I'm just
using the Button_Click event:

private void Button1_Click(object sender, System.EventArgs e)
{
try
{
oleDbConnection1.Open();
if (name.Text.Trim().Length>0 &&
msg.Text.Trim().Length>0)
{
oleDbDataAdapter1.InsertCommand.CommandText =
"INSERT INTO [msg log] "+
"(hour, name, msg) "+
"VALUES ("+
"'"+DateTime.Now.ToString()+"', "+
"'"+name.Text.Trim()+"', "+
"'"+msg.Text.Trim()+"')";
oleDbDataAdapter1.InsertCommand.ExecuteNonQuery();
name.Text = "";
msg.Text = "";
}
oleDbDataAdapter1.SelectCommand.CommandText =
"SELECT hora, nome, msg FROM [msg log]";
oleDbDataAdapter1.Fill(dataset11);
oleDbConnection1.Close();
DataGrid1.DataBind();
}
catch (System.Data.OleDb.OleDbException exception)
{
Error.Visible = true;
Error.Text = "Error on server: " + exception.ToString();
}
}

And here is my PageIndexChanged Event:

private void DataGrid1_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
}

Please, let me know if there is something very wrong with it.

Thanks.
Ruy.
 
D

Dave

Yep, that'll do it to!

Sorry I couldn't help.

--
Dave Sexton
[email protected]
-----------------------------------------------------------------------
Ruy Castelli said:
Thanks Dave,

I followed your tips, but it still didn't work. Then I was checking some configuration and I noticed that somehow the document
properties "enableSessionState" and "enableViewState" were set to false. When I set then to true, everything worked.

Thanks.
Ruy.

Dave said:
I noticed that your are binding twice in the DataGrid1_PageIndexChanged event handler. Remove the last line and it should work
fine.

My last post was submitted because I searched for IsPostBack, and didn't find it... I assume this is a type-o?
if (!PostBack)

Should be:

if (!IsPostBack)

--
Dave Sexton
[email protected]
-----------------------------------------------------------------------
Ruy Castelli said:
Hello Dave, thanks for your answer.

Now, I'm binding data on the Page_Loads and PageIndexChange events, but the problem is worse, when I click on Next, the datagrid
disapears.

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (!PostBack)
{
try
{
oleDbConnection1.Open();

oleDbDataAdapter1.SelectCommand.CommandText =
"SELECT hour, name, msg FROM [msg log]";

oleDbDataAdapter1.Fill(dataset11);
DataGrid1.DataBind();
}
catch (System.Data.OleDb.OleDbException exception)
{
Error.Visible = true;
Error.Text = "Server error: " + exception.ToString();
}
finally
{
oleDbConnection1.Close();
}
}
}

Here is my PageIndexChanged event:

private void DataGrid1_PageIndexChanged(
object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;

try
{
oleDbConnection1.Open();

oleDbDataAdapter1.SelectCommand.CommandText =
"SELECT hour, name, msg FROM [msg log]";

oleDbDataAdapter1.Fill(dataset11);
DataGrid1.DataBind();
}
catch (System.Data.OleDb.OleDbException exception)
{
Error.Visible = true;
Error.Text = "Server error: " + exception.ToString();
}
finally
{
oleDbConnection1.Close();
}

DataGrid1.DataBind();
}


"Dave" <[email protected]> escreveu na mensagem private void DataGrid1_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
}


Where is your DataSource??


--
Dave Sexton
[email protected]
-----------------------------------------------------------------------
Hello,

I'm learning to code in C# and I created a DataGrid component, which I couldn't get it to work properly either using "Next >>"
and "<< Previous" buttons or using actual page numbers.

When I use the "Next" and "Previous" buttons, I move on one page (going from page 1 to 2). Then if I click next on page 2, it
comes back to page 2. I checked my code several times and I can't find anything wrong with it. If I click on Previous, it
comes back to page 1.

When I use the actual page numbers, on the very first time the applications shows all possible page numbers possible (1 to 10
maybe). Then if I click on page 2, it refreshes and shows the correct number of pages (4) and only links 1 to 4 are displayed.
Now, I can access any page I want and the application displays it properly, the problem is on the first time it displays the
data.

Basically, it's a page when 2 TextBoxes and one Button. When the user clicks on this button, the application adds data to a
database and displays all rows.

I'm not using Page_Load for that, let me know if I'm wrong. I'm just using the Button_Click event:

private void Button1_Click(object sender, System.EventArgs e)
{
try
{
oleDbConnection1.Open();
if (name.Text.Trim().Length>0 &&
msg.Text.Trim().Length>0)
{
oleDbDataAdapter1.InsertCommand.CommandText =
"INSERT INTO [msg log] "+
"(hour, name, msg) "+
"VALUES ("+
"'"+DateTime.Now.ToString()+"', "+
"'"+name.Text.Trim()+"', "+
"'"+msg.Text.Trim()+"')";
oleDbDataAdapter1.InsertCommand.ExecuteNonQuery();
name.Text = "";
msg.Text = "";
}
oleDbDataAdapter1.SelectCommand.CommandText =
"SELECT hora, nome, msg FROM [msg log]";
oleDbDataAdapter1.Fill(dataset11);
oleDbConnection1.Close();
DataGrid1.DataBind();
}
catch (System.Data.OleDb.OleDbException exception)
{
Error.Visible = true;
Error.Text = "Error on server: " + exception.ToString();
}
}

And here is my PageIndexChanged Event:

private void DataGrid1_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
}

Please, let me know if there is something very wrong with it.

Thanks.
Ruy.
 

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

Top