473,387 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

DataGrid - Paging problems

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.DataGridPageChangedEvent Args e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
}

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

Thanks.
Ruy.
Nov 19 '05 #1
1 1444
Hi Ruy,

In PageIndexChanged event the code should be:

DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataSource = DataSourceObject; // You have to
re-bind data source
DataGrid1.DataBind();

And yo should also have datagrid's viewstate enabled.

HTH

Elton Wang
el********@hotmail.com

-----Original Message-----
Hello,

I'm learning to code in C# and I created a DataGrid component, which Icouldn'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 frompage 1 to 2). Then if I click next on page 2, it comes back to page 2. Ichecked my code several times and I can't find anything wrong with it. If Iclick on Previous, it comes back to page 1.

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

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

I'm not using Page_Load for that, let me know if I'm wrong. I'm just usingthe 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.DataGridPageChangedEven tArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
}

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

Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Joseph D. DeJohn | last post by:
I am trying to get pagination working on a datagrid. Can anyone point me to a resource for help on this? I'm not sure if custom paging is the best option or not.
4
by: Coleen | last post by:
Sorry about the multi-posting, but it does not seem as though the other newsgroups are frequented enough to get any advice on this problem...If anyone can possibly help, I would greatly appreciate...
2
by: RJN | last post by:
Hi Sorry for posting again. I have a datagrid which is put inside a div tag to make it scrollable. I need to page the datagrid. The page numbers appear at the bottom of the datagrid and has...
1
by: Danny Ni | last post by:
Hi, We use ASP.Net datagrid paging a lot and we have been experiencing "Index out of bound" error messages when we click on rows of datagrid on differnet pages every now and then. The...
6
by: Shawn | last post by:
Hi. I already have a datagrid where I'm using paging. I have a stored procedure that fills a temp table with 200-500 rows and then sends back 10 records at the time. When I go to page 2 the SP...
2
by: Axel Dahmen | last post by:
Hi, I'm using a DataGrid control to show a table's content with paging. For navigation through the pages I'm using the DataGrid's intrinsic navigation section. My problem: The DataGrid...
5
by: tshad | last post by:
Is there a way to carry data that I have already read from the datagrid from page to page? I am looking at my Datagrid that I page through and when the user says get the next page, I have to go...
0
by: Pat | last post by:
I have 3 Datagrid nested. Master Details Child The master has paging (And i'm using the paging inbuilt in the Datagrid) in the Master DataGrid you select a linkbutton(using commandname) and it...
6
by: coleenholley | last post by:
Hi All :-) I need to populate a table I created as a web form. Are there any links to show me how to do this? I CANNOT use a dtagrid for this, the table has to be laid out as follows: Header...
3
by: GD | last post by:
Got a simple ASPX page (.NET v1.1) with a DataGrid displaying a list of widgets. If the page has a QueryString called WidgetId specific, I need to automatically mark the specific DataGridItem as...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.