Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying A Particular Page of a Datagrid 1

Status
Not open for further replies.

Juice05

Programmer
Dec 4, 2001
247
US
I have a datagrid on an asp.net page, the end user can add records to it. The grid has paging enabled...10 items per page.

The grid is sorted by date. If I am displaying the first page of the grid, entering the eleventh record, that has the maximum date (Out of all list thus far), when the record is inserted into the datagrid, the current page remains 1. I have users who are complaining that they can't find their records after they have added or updated them.

If that was too confusing maybe this summary makes sense:

Does anyone know of a way to ensure that after an update or insert, the page with the last edited/added record is displayed?
 
jbenson001,

Actually, I am inserting the record into the database and then repopulating the datagrid.
 
You'll have to set the page index to the last page.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I see, the problem is you need to know an "id" of the row enterd and on what page it would be on, then you can set the CurrentPageIndex.

Jim
 
As the OP said the user wanted to go to the last insterted record, I believe you can just check the PageCount of the DataGrid and navigate to that.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
ca8msm yes that would be true I guess it it were a blank row, but if it were sorted somehow, with data, that they would need to find where it is
 
Good point


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
It is sorted by date, so I need to find it in the table before I set the currentpageindex for my datagrid.

I am currently trying to find it in the table by looping through the records. When I find it I am going to divide the record count at that point by 10 and ceiling that to come up with a page index.

See any issues with that?
 
OK...the code runs fine and it appears all of the right values are there but The currentpageindex isn't changing. Is this because I am loading the datagrid before i set the currentpageindex? Should I set the index then load the grid?
 
Probably - just try it and see what happens.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
OK, that works.

So it now finds the value within the table and calculates where in the datagrid the record is located and sets the currentpageindex = to that.

I am getting the error "Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount." when I add the 11, 21, 31, 41, etc. record. Obviously the page index doesn't exist yet. How can I address this issue? The only thing I can come up with is bind the table then set the index, then bind the table again. This doesn't seem like a logical thing to do, but I bet it would work.
 
I was wrong...it didn't work. See, this is why I should never bet.
 
It works, thanks for all your help.

Here is the code I used to get this to work:

Loading the Grid
Code:
Public Sub LoadAuthHours(Optional ByVal TimeID As Integer = 0)
        Dim ds As DataTable = DataAccess.GetAuthHours(AuthID)

        Dim tbl As DataTable = ds
       
        For i As Integer = 0 To tbl.Rows.Count - 1
            Dim rw As DataRow = tbl.Rows(i)
            If rw("AuthorizationsTimeID") = TimeID Then
                dgHours.CurrentPageIndex = Math.Ceiling((i + 1) / 10) - 1
                Exit For
            End If
        Next

        dgHours.DataSource = ds
        dgHours.DataBind()
    End Sub

From the Insert Command
Code:
LoadAuthHours(NewID) 'NewID is returned from the DataAccess Layer (ID of the added record)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top