Datagrid PageCount Nightmare!!

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

Hello All,

I am having a nightmare trying to add a new row to my
Datagrid. When I use the code below I get the
error: 'Invalid CurrentPageIndex value. It must be >= 0
and < the PageCount. Can anyone help me with this?

Dim dg2 as DataGrid = DG.Items(0).FindControl("dg2")
DSClass = Session("DSClass")
dt = DSClass.Tables(0)
dim dr as DataRow = dt.NewRow ()
dt.Rows.Add(dr)
Session("DSClass") = DSClass
dg2.DataSource = Session("DSClass")

intIndex = dg2.Items.Count ''intIndex = 7

If intIndex >= dg2.PageSize Then ''Page Size = 7

''At present CurrentPageIndex = 0

dg2.CurrentPageIndex = dg2.CurrentPageIndex + 1
intIndex = 0

''CurrentPageIndex = 1 And PageCount = 1

End if

dg2.EditItemIndex = intIndex

dg2.Datasource = Session("DSClass").Tables(0)
dg2.DataBind()
''Errors here because CurrentPageIndex = 1 and needs to be
less than 1, but this is not the page I want to be
displayed?
 
Try setting the CurrentPageIndex to zero before you add the new row.

I encountered the "'Invalid CurrentPageIndex value. It must be >= 0 > and <
the PageCount" in the following scenario.

I bind my DataGrid to my DataSet and go to Page 2. Let's say I rebind to a
new DataSet which would only contain on Page of records, but the DataGrid is
still at Page 2. I'd get the above error.

So before I rebind, I always set the CurrentPageIndex to 0
 
Hi,
Thanks for replying,

I see what you're saying here but when I do this I end up
not editing the row I need,
I need to be editing the first record of the last page,
when I set the current page index to 0 I end up editing
the first record of the first page?
 
Back
Top