DataGrid - Paging problems



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
elton_wang@xxxxxxxxxxx


>-----Original Message-----
>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.
>
>
>.
>
.


Loading