Normally no code needed in PageIndexChanging event. How do you data bind? Using a DataSource control or from Code behind?
I do data bind from Code behind.
I had to make a simple TEST page and fixed the problem by having a call to the function that displays the data in addition to making a call to databind() via PageIndexChanging event. That is, I have 2 DataBind() calls!!! It works but why I have to put
two calls!!!
In my original page, I also have to display some calculations in the footer. Because of enabling the paging, it gave me problems!!!
Will paging work while displaying some data in the footer?
My problem now, why paging will casue a problem if there is some calculations to be displayed in the footer?
In the page load event, I have the followings :
If String.IsNullOrEmpty(Session("ReadWriteAccess")) = False Then
Call PopulateMenu()
End If
If Not IsPostBack Then
Call GetRegions()
Call DoUserOptions()
else
Call DoUserOptions()
End If
This is the error msg I get if I enable paging.
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Dim index As Integer = e.Row.DataItemIndex
Dim SprVsnEstCst, PwrSprtCstEstmt, CntnCstEstmt, MtrDpstEstmt As Decimal If Not GridView1.DataKeys(index).Item(0) Is DBNull.Value Then
SprVsnEstCst = Convert.ToDecimal(GridView1.DataKeys(index).Item(0))
Else
I have created a very simple test page and it shows the same error msg. Here is the full code of the code behind :
Imports System.Data
Imports System.Data.SqlClient
Imports System.Diagnostics
Imports System.Windows.Forms.DataGrid
Imports System.Web.Security
Imports System.Transactions
Partial Class paging
Inherits System.Web.UI.Page
Public strConnection As String = System.Configuration.ConfigurationSettings.AppSettings("ConnectionString")
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Call DisplayList()
End Sub
Sub DisplayList()
Dim connString As String = strConnection
Dim sql As String = " SELECT * FROM Accounts"
Dim conn As SqlConnection = New SqlConnection(strConnection)
Dim objcmd As SqlCommand = New SqlCommand(sql, conn)
Dim objda As New SqlDataAdapter
objda.SelectCommand = objcmd
Try
conn.Open()
Dim objds As DataSet = New DataSet
objda.Fill(objds, "0")
Me.GridView1.DataSource = objds.Tables("0")
If objds.Tables("0").Rows.Count > 0 Then
GridView1.DataBind()
End If
conn.Close()
Catch ex As Exception
Response.Write(ex)
End Try
End Sub
Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
GridView1.PageIndex = e.NewPageIndex
GridView1.DataBind()
End Sub
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.Header Then
do nothing
ElseIf e.Row.RowType = DataControlRowType.DataRow Then
Dim index As Integer = e.Row.DataItemIndex
Dim MyString As String
If Not GridView1.DataKeys(index).Item(0) Is DBNull.Value Then
MyString = GridView1.DataKeys(index).Item(0)
Else
MyString = "no value"
End If
End If
End Sub
End Class
When I click on a page number, I get the error at line
If Not GridView1.DataKeys(index).Item(0) Is DBNull.Value Then
The err:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Sub fillGV()
Dim constr As String = ConfigurationManager.ConnectionStrings("TestConnectionString").ConnectionString
Dim conn As New SqlConnection(constr)
conn.Open()
Dim qry As String = "SELECT * FROM [timecheck]"
Dim cmd As New SqlCommand(qry, conn)
Dim da As New SqlDataAdapter(cmd)
Dim dt As New DataTable
da.Fill(dt)
GridView2.DataSource = dt
GridView2.DataBind()
End Sub
Protected Sub Page_Load(sender As Object, e As System.EventArgs)
fillGV()
End Sub
Protected Sub GridView2_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewPageEventArgs)
GridView2.PageIndex = e.NewPageIndex
GridView2.DataBind()
End Sub
Protected Sub GridView2_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim index As Integer = e.Row.RowIndex
Dim MyString As String
MyString = GridView2.DataKeys(index)(0).ToString
Response.Write(MyString + "<br>")
End If
End Sub
Kindly mark this post as "Answer", if it helped you.
(Talk less..Work more)
Marked as answer by Noorstudio on May 17, 2012 10:29 AM
Noorstudio
Member
130 Points
243 Posts
Enabling Paging in GridView
May 16, 2012 02:30 PM|LINK
Hello!
When clicking on displayed pages, no data is displayed!! What I am missing?
1. I set allowpaging property of grid view to true
2. I set pagesize property to number of records wanted in each page.
3. In pageindex changing event, I wrote the following code
gridview1.pageindex=e.newpageindex
gridview1.databind()
Thanks
_Manvel_
Contributor
4242 Points
923 Posts
Re: Enabling Paging in GridView
May 16, 2012 02:45 PM|LINK
You missed to set DataSource for GridView in step 3, so you should have
basheerkal
Star
10672 Points
2426 Posts
Re: Enabling Paging in GridView
May 16, 2012 02:46 PM|LINK
Normally no code needed in PageIndexChanging event. How do you data bind? Using a DataSource control or from Code behind?
(Talk less..Work more)
Noorstudio
Member
130 Points
243 Posts
Re: Enabling Paging in GridView
May 16, 2012 05:31 PM|LINK
I do data bind from Code behind.
I had to make a simple TEST page and fixed the problem by having a call to the function that displays the data in addition to making a call to databind() via PageIndexChanging event. That is, I have 2 DataBind() calls!!! It works but why I have to put two calls!!!
In my original page, I also have to display some calculations in the footer. Because of enabling the paging, it gave me problems!!!
Will paging work while displaying some data in the footer?
Thanks
basheerkal
Star
10672 Points
2426 Posts
Re: Enabling Paging in GridView
May 16, 2012 06:06 PM|LINK
If you databind in code behind the the way you handled the PageIndexChanging event is correct.
Check is it an issue of wrong handling of postback. What is in your Page_Load event?
(Talk less..Work more)
Noorstudio
Member
130 Points
243 Posts
Re: Enabling Paging in GridView
May 16, 2012 08:52 PM|LINK
Hello!
My problem now, why paging will casue a problem if there is some calculations to be displayed in the footer?
In the page load event, I have the followings :
If String.IsNullOrEmpty(Session("ReadWriteAccess")) = False Then
Call PopulateMenu()
End If
If Not IsPostBack Then
Call GetRegions()
Call DoUserOptions()
else
Call DoUserOptions()
End If
This is the error msg I get if I enable paging.
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Dim index As Integer = e.Row.DataItemIndex
Dim SprVsnEstCst, PwrSprtCstEstmt, CntnCstEstmt, MtrDpstEstmt As Decimal
If Not GridView1.DataKeys(index).Item(0) Is DBNull.Value Then
SprVsnEstCst = Convert.ToDecimal(GridView1.DataKeys(index).Item(0))
Else
Thanks
basheerkal
Star
10672 Points
2426 Posts
Re: Enabling Paging in GridView
May 17, 2012 02:07 AM|LINK
Which Sub do you use to DataBind?
Are you doing it only if PostBack is not true? If so On postback GridView will be empty.
(Talk less..Work more)
Noorstudio
Member
130 Points
243 Posts
Re: Enabling Paging in GridView
May 17, 2012 07:46 AM|LINK
Hello!
I have created a very simple test page and it shows the same error msg. Here is the full code of the code behind :
Imports System.Data Imports System.Data.SqlClient Imports System.Diagnostics Imports System.Windows.Forms.DataGrid Imports System.Web.Security Imports System.Transactions Partial Class paging Inherits System.Web.UI.Page Public strConnection As String = System.Configuration.ConfigurationSettings.AppSettings("ConnectionString") Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Call DisplayList() End Sub Sub DisplayList() Dim connString As String = strConnection Dim sql As String = " SELECT * FROM Accounts" Dim conn As SqlConnection = New SqlConnection(strConnection) Dim objcmd As SqlCommand = New SqlCommand(sql, conn) Dim objda As New SqlDataAdapter objda.SelectCommand = objcmd Try conn.Open() Dim objds As DataSet = New DataSet objda.Fill(objds, "0") Me.GridView1.DataSource = objds.Tables("0") If objds.Tables("0").Rows.Count > 0 Then GridView1.DataBind() End If conn.Close() Catch ex As Exception Response.Write(ex) End Try End Sub Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging GridView1.PageIndex = e.NewPageIndex GridView1.DataBind() End Sub Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound If e.Row.RowType = DataControlRowType.Header Then do nothing ElseIf e.Row.RowType = DataControlRowType.DataRow Then Dim index As Integer = e.Row.DataItemIndex Dim MyString As String If Not GridView1.DataKeys(index).Item(0) Is DBNull.Value Then MyString = GridView1.DataKeys(index).Item(0) Else MyString = "no value" End If End If End Sub End ClassWhen I click on a page number, I get the error at line
The err:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Thanks
TimoYang
Contributor
3732 Points
1275 Posts
Re: Enabling Paging in GridView
May 17, 2012 08:19 AM|LINK
Check whether you've assigned DataKeyNames successfully!
basheerkal
Star
10672 Points
2426 Posts
Re: Enabling Paging in GridView
May 17, 2012 08:50 AM|LINK
OK,
Thisis my test page. It is not giving any error.
Form
<form id="form1" runat="server"> <div> <asp:GridView ID="GridView2" runat="server" AllowPaging="True" onpageindexchanging="GridView2_PageIndexChanging" PageSize="4" ShowFooter="True" onrowdatabound="GridView2_RowDataBound" DataKeyNames="insertedby"> </asp:GridView> </div> </form>Code behind
Sub fillGV() Dim constr As String = ConfigurationManager.ConnectionStrings("TestConnectionString").ConnectionString Dim conn As New SqlConnection(constr) conn.Open() Dim qry As String = "SELECT * FROM [timecheck]" Dim cmd As New SqlCommand(qry, conn) Dim da As New SqlDataAdapter(cmd) Dim dt As New DataTable da.Fill(dt) GridView2.DataSource = dt GridView2.DataBind() End Sub Protected Sub Page_Load(sender As Object, e As System.EventArgs) fillGV() End Sub Protected Sub GridView2_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewPageEventArgs) GridView2.PageIndex = e.NewPageIndex GridView2.DataBind() End Sub Protected Sub GridView2_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) If e.Row.RowType = DataControlRowType.DataRow Then Dim index As Integer = e.Row.RowIndex Dim MyString As String MyString = GridView2.DataKeys(index)(0).ToString Response.Write(MyString + "<br>") End If End Sub(Talk less..Work more)