Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Weird issue with pager of datagridview

Author  Topic 

jhermiz

3564 Posts

Posted - 2007-01-10 : 10:52:59
I was playing with the paging technique for sql server 2k5 (article posted by graz on the main page). I setup my datagridview (visual studio.net 2k5 btw) to allow for paging. I want my page size to be 5 and to start with page number one.

I wrote up some code to pass in the page size and page number into a sproc:


Imports System.Data.SqlClient
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
BindData(5, 1)
End If
End Sub
Public Sub BindData(ByVal pSize As Integer, ByVal pNumber As Integer)
Dim sConn As SqlConnection
Dim cmdSelect As SqlCommand
Dim da As SqlDataAdapter
Dim ds As Data.DataSet

Try
sConn = New SqlConnection("User ID=joeblow;Password=somePass;Initial Catalog=CData;Data Source=SQL-1;")
cmdSelect = New SqlCommand("selData", sConn)

With cmdSelect
.CommandType = Data.CommandType.StoredProcedure
'add parameters
.Parameters.Add("@PageSize", Data.SqlDbType.Int).Value = pSize
.Parameters.Add("@PageNumber", Data.SqlDbType.Int).Value = pNumber
da = New SqlDataAdapter
da.SelectCommand = cmdSelect
ds = New Data.DataSet
da.Fill(ds)
End With

With Me.GridView1
.DataSource = ds.Tables(0)
If ds.Tables(0).Rows.Count = 0 Then
Me.GridView1.Visible = False
GridView1.DataSource = Nothing
End If
.DataBind()
.Visible = True
.AllowPaging = True
End With

Catch e As Exception
Response.Write("An Error Occurred: " & e.ToString())
'clean up and close resources
Finally
ds = Nothing
da = Nothing
cmdSelect = Nothing
sConn.Close()
sConn = Nothing
End Try
End Sub

Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
Me.GridView1.PageIndex = e.NewPageIndex
BindData(5, e.NewPageIndex + 1)
End Sub
End Class


When I run the code, the grid displays the first 5 results which looks good. I have about 20 test records in the database. The issue is I do not see the pager on the bottom of the datagridview. I do see it when I modify my code to set the pagesize to 100...Then I see the page numbers 1,2,3,4,... etc. However why would that be ? I dont want to set the page size to 100 as I only want to see 5 rows per page. Or is the page size referring to something different ?




Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]

RS Blog -- [url]http://weblogs.sqlteam.com/jhermiz[/url]

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-01-10 : 11:22:18
I think you'll need to create your own next/last/first/etc buttons and so on, since you are handling the paging server-side. If not all of the rows are returned to the DataGridView, it has no way of knowing how many rows it is dealing with and whether or not it should be displaying the paging info, since as far as it is concerned there is only as many total rows as it is getting.

- Jeff
Go to Top of Page

jhermiz

3564 Posts

Posted - 2007-01-10 : 11:29:58
I thought that was the case, because in the past I used the "enable paging" property. I just kept thinking to myself maybe it is possible..or at least hopefully it was possible.

Thank you.

Jon



Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]

RS Blog -- [url]http://weblogs.sqlteam.com/jhermiz[/url]
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-01-10 : 17:28:13
don't you just love that advanced functionality like paging works so beautifully wrong.




Go with the flow & have fun! Else fight the flow
blog thingie: http://weblogs.sqlteam.com/mladenp
Go to Top of Page
   

- Advertisement -