DataGrid Paging??

D

DaMan

I have code that builds a dataset and connects it to a datagrid. I set up
paging but something is broke.
I can do a pagecount property and it returns 6
I tried to use the simple paging example in MSDN and it only will display
the first page, if I hit the next page button it (the datagrid is not
displayed.)
I can do it manually but only once.

label.text = DataGrid1.PageCount <<returns 6
DataAdapter.Fill(tmpDataSet)
DataGrid1.CurrentPageIndex = 0
DataGrid1.DataBind()
*********Great, displays 1 page****
I can set pageindex to 1 or 2 or 3 or ...6 and get the correct data for that
page....

BUT, I can only do this once, if I display, say, page one or whatever, and
change the pageindex to another page, the datagrid disappears.. I must be
missing something, but I cant figure out what....
Ex Code in PAGE_LOAD

label.text = DataGrid1.PageCount <<returns 6
DataAdapter.Fill(tmpDataSet)
DataGrid1.CurrentPageIndex = 0 << I can set to any nbr up to 5 and get
that page..
DataGrid1.DataBind()
and create a button that simply changes page index
DataGrid1.CurrentPageIndex = 2 << no number works here now after page loads
DataGrid1.DataBind()
********nothing is displayed when I hit button

Please Help Thanks KT
 
M

Mike Moore [MSFT]

Hi,

You wrote that you're using the page up and down keys. Here is how I
created a grid which uses the page up & down keys. Please let me know if
this code snippet answers your question.

*** ASPX page
<script>
function window_onkeydown() {
if (event.keyCode==33 || event.keyCode==34)
{
Form1.UpDown.value=event.keyCode;
Form1.submit();
}
}
</script>
</HEAD>
<body onkeydown="return window_onkeydown()">

*** Code-behind
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.AllowPaging = True
DataGrid1.PageSize = 10
DataGrid1.PagerStyle.Mode = PagerMode.NextPrev
DataGrid1.PagerStyle.NextPageText = "Next"
DataGrid1.PagerStyle.PrevPageText = "Prev"
Bind()
End If
If Request.Form("UpDown") <> "" Then
If Request.Form("UpDown") = 33 Then
If DataGrid1.CurrentPageIndex < DataGrid1.PageCount - 1 Then
DataGrid1.CurrentPageIndex = DataGrid1.CurrentPageIndex + 1
End If
Else
If DataGrid1.CurrentPageIndex > 0 Then
DataGrid1.CurrentPageIndex = DataGrid1.CurrentPageIndex - 1
End If
End If
End If
Bind()
End Sub

Private Sub Bind()
Dim connectionString As String = "server='localhost';
trusted_connection=true; Database='pubs'"
Dim cnn As SqlConnection = New
System.Data.SqlClient.SqlConnection(connectionString)
Dim queryString As String = "SELECT [authors].[au_id],
[authors].[au_lname], [authors].[au_fname] FROM [authors]"
Dim da As SqlDataAdapter = New SqlDataAdapter(queryString, cnn)
Dim ds As New DataSet()
da.Fill(ds, "pubs")
DataGrid1.DataSource = ds.Tables("pubs")
DataGrid1.DataBind()
da.Dispose()
cnn.Close()
cnn.Dispose()
End Sub

Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, ByVal e
As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles
DataGrid1.PageIndexChanged
DataGrid1.CurrentPageIndex = e.NewPageIndex
Bind()
End Sub


Thank you, Mike Moore
Microsoft, ASP.NET

This posting is provided "AS IS", with no warranties, and confers no rights.


--------------------
 

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,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top