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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGrid Paging 3

Status
Not open for further replies.

dvannoy

MIS
Joined
May 4, 2001
Messages
2,765
Location
US
Users insert records directly into a grid. the grid id set to allow 10 items per page. I am trying to get the grid to go directly to the next page after the 10th record is entered..

Sub dgDetail_Paged(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs)
dgDetail.CurrentPageIndex = e.NewPageIndex
BindGrid()
End Sub

Html

OnPageIndexChanged="dgDetail_Paged"


any help would be appreciated.

 
Need to add that after the line of code where you update the database and bind with the new dataset containing the new record. The record could be anywhere depending on the sort order.

private MyNewRecord_ID

private UpdateButton_Clicked()

BindGrid()

Dim tbl As DataTable = CType(dgDetail.DataSource, DataTable)
'Dim tbl As DataTable = CType(dgDetail.DataSource, Dataview).table
'Dim tbl As DataTable = CType(dgDetail.DataSource, Dataset).tables(n)
For i As Integer = 0 To tbl.Rows.Count - 1
Dim rw As DataRow = tbl.Rows(0)
If rw("MyIDColumn") = MyNewRecord_ID Then
dgDetail.CurrentPageIndex = i
Exit For
End If
Next

end sub
 
I get specified cast is not valid


Dim tbl As DataTable = CType(dgDetail.DataSource, DataTable)


Dim tbl As DataTable = CType(dgDetail.DataSource, DataTable)

Dim i As Integer

For i = 0 To tbl.Rows.Count - 1
Dim rw As DataRow = tbl.Rows(0)
If rw("ID") = ID Then
dgDetail.CurrentPageIndex = i
Exit For
End If
Next

This is in my Insert Sub after the grid binds.

 
It could be one of the three
Dim tbl As DataTable = CType(dgDetail.DataSource, DataTable)
'Dim tbl As DataTable = CType(dgDetail.DataSource, Dataview).table
'Dim tbl As DataTable = CType(dgDetail.DataSource, Dataset).tables(n)
 
where are you declaring (n) at?

sorry for the confusion

 
n is the index of the table in the collection.. 0,1,2.. etc
you may be able to use the table name as well
 
n" is just the index of the relevant table you want to use


____________________________________________________________

Need help finding an answer?

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

 
I am using SqlDataReader to bind my grid...could that be the problem?

 
If you are using a DataReader, then obviously you'll have to modify the examples posted by RTomes as they were for a Data Table/View/Set (and a DataReader only allows you to move forward through the results once).


____________________________________________________________

Need help finding an answer?

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

 
OK....changed to dataadapter and it worked.. the grid holds 10 rows per page. once the 10 rows have been added, how can i get the grid to auto next page..the next page link at the bottom of the grdi highlites and if you click on it , it goes to the 11th record. can that been done automatic?

I really appreciate both your help

 
Just call the sub that handles the next page link


____________________________________________________________

Need help finding an answer?

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

 
I do that and I get nothing.. sorry for the confusion..

should this be in my insert statement?

Dim tbl As DataTable = CType(dgDetail.DataSource, DataTable)

Dim i As Integer

For i = 0 To tbl.Rows.Count - 1
Dim rw As DataRow = tbl.Rows(0)
If rw("ID") = ID Then
dgDetail.CurrentPageIndex = i
Exit For
End If
Next

Sub dgDetail_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dgDetail.PageIndexChanged

dgDetail.CurrentPageIndex = e.NewPageIndex
BindPOGrid()

End Sub




 
I am still having problems with this..whatever I do, I cannot get this to work.

 
is view state enabled???

Known is handfull, Unknown is worldfull
 
I know I am missing something here...

If rw("MyIDColumn") = MyNewRecord_ID Then

MyIDColumn and MyNewRecord_ID is the key field in my DB correct??



 
is this what u want?

i have used the ViewState object to do it...

<%@ debug="true" enableviewstate="true"%>
<script runat="server">
Dim TheList as new ArrayList
sub page_load
dim i as integer
for i=1 to 6
TheList.Add(i)
next
if not ispostback then
TheGrid.DataSource=TheList
TheGrid.DataBind
ViewState("TheList")=TheList
end if
end sub

sub ChangePage(s as object, e as DataGridPageChangedEventArgs)
TheList=ViewState("TheList")
TheGrid.CurrentPageIndex = e.NewPageIndex
TheGrid.DataSource=TheList
TheGrid.DataBind
end sub

sub AddMe(sender As Object, e As System.EventArgs)
Dim OldList as ArrayList=ViewState("TheList")
OldList.Add(OldList.count+1)
dim TheNewPage as integer=TheGrid.CurrentPageIndex+1
if(OldList.count > TheGrid.PageCount * TheGrid.PageSize) then
TheGrid.CurrentPageIndex=TheNewPage
end if
TheGrid.DataSource=OldList
TheGrid.DataBind
ViewState("TheList")=OldList
end sub

</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form runat="server">
<asp:DataGrid id="TheGrid" runat="server" AllowPaging="true" PageSize="5" OnPageIndexChanged="ChangePage" EnableViewState="true">
</asp:DataGrid>
<input type="submit" value="Add" runat="server" onserverclick="AddMe">
</form>
</body>
</html>


Known is handfull, Unknown is worldfull
 

still wont page to the next page..

what does this mean?

<input type="submit" value="Add" runat="server" onserverclick="AddMe">

I add the records from a button in the grid

<asp:ImageButton id="btnAdd" runat="server" ImageUrl="./images\btnAdd.gif" CommandName="Insert"></asp:ImageButton>




 
AddMe will add records to the grid.

i have just given an example for using view state property...

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top