DataGrid Paging - Index out of Range Error

B

Bill Byrd

I am attempting to implement paging with a DataGrid. The
control populates fine. The < > paging links appear.

However when I click the > to go to the next set of
information I receive "Server Error in ... Application.
Index was out of range. Must be non-negative and less than
the size of the collection. Parameter name: index.

The code in my PageIndexChanged sub is not being reached.

The > link does a __doPostBack("DataGrid1:ctl8:ctl1","")

I can not find the where or why of this error.

Please email me with you thoughts ro ideas.

Thanks - Bill
 
M

Mike Moore [MSFT]

Hi Bill,

Please try this sample code. It may lead you in the right direction.

* Place a datagrid on a form
* Add this code.

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
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
Microsoft, ASP.NET Support Professional

Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer’s security.

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,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top