Carrying data for a Datagrid from page to page

T

tshad

Is there a way to carry data that I have already read from the datagrid from
page to page?

I am looking at my Datagrid that I page through and when the user says get
the next page, I have to go to the database to get the next page. Is there
a way to use the dataset to allow us to read back and forth in it instead of
going back to the database to get it?

Thanks,

Tom
 
S

Scott M.

You would write the code that goes to the database and gets the data in the
Not IsPostBack and let ViewState carry the data from that point forward.

Sub Page_Load
'Set DataGrid's data source

If Not IsPostBack Then
'Go to database and get data
'Bind DataGrid to data
End If


End Sub
 
T

tshad

Scott M. said:
You would write the code that goes to the database and gets the data in the
Not IsPostBack and let ViewState carry the data from that point forward.

Sub Page_Load
'Set DataGrid's data source

If Not IsPostBack Then
'Go to database and get data
'Bind DataGrid to data
End If


End Sub

But where is the data stored?

I am not putting all the data in the grid when I use paging with the
Datagrid.

Tom
 
S

Scott M.

First, yes you are bringing all the data into the grid when you use paging
(unless you are using custom paging), you are only showing one page of data
at a time, but you are, in fact bringing all the data down to the grid.

All of the Web Form controls (DataGrid included) persist their own state
between page requests via ViewState. ViewState is a hidden form field that
contains the entire page's state encoded and compressed into this string.
After you go and get the data on the first page call, ALL of the data is
placed into the ViewState hidden form field. On the next page call, the
data is taken out of ViewState and placed back into the various controls
that had the data in the first place. This is why you only need to go to
the data on the first page call.

If you are using the Custom Paging feature, you are going to be only
retrieving one page of data at a time and so in this circumstance you would
have to go back to the database on each page call to get the next set of
records.
 
T

tshad

Scott M. said:
First, yes you are bringing all the data into the grid when you use paging
(unless you are using custom paging), you are only showing one page of
data at a time, but you are, in fact bringing all the data down to the
grid.

All of the Web Form controls (DataGrid included) persist their own state
between page requests via ViewState. ViewState is a hidden form field
that contains the entire page's state encoded and compressed into this
string. After you go and get the data on the first page call, ALL of the
data is placed into the ViewState hidden form field. On the next page
call, the data is taken out of ViewState and placed back into the various
controls that had the data in the first place. This is why you only need
to go to the data on the first page call.

If you are using the Custom Paging feature, you are going to be only
retrieving one page of data at a time and so in this circumstance you
would have to go back to the database on each page call to get the next
set of records.


I'm not using the Custom Paging Feature, but I am calling my BindData
procedure to get the data each time. The following procedure is linked to
my first/next/previous/last links and sets the CurrentPageIndex accordingly.
The last line calls the BindData procedure and it works fine. But if what
you say is true, I am doing a lot of extra work (and slowing the page down,
I would assume). The problem is that if I comment out the BindData routine
the Datagrid comes back exactly the same as it did before post.

**********************************************************
Sub PagerButtonClick(sender As Object, e As EventArgs)
trace.warn("in PagerButtonClick")
'used by external paging UI
Dim arg As String = sender.CommandArgument
Dim oGrid = DataGrid1

Select arg
Case "next": 'The next Button was Clicked
If (oGrid.CurrentPageIndex < (oGrid.PageCount - 1)) Then
oGrid.CurrentPageIndex += 1
End If

Case "prev": 'The prev button was clicked
If (oGrid.CurrentPageIndex > 0) Then
oGrid.CurrentPageIndex -= 1
End If

Case "last": 'The Last Page button was clicked
oGrid.CurrentPageIndex = (oGrid.PageCount - 1)

Case Else: 'The First Page button was clicked
oGrid.CurrentPageIndex = Convert.ToInt32(arg)
End Select

'Now, bind the data!
' BindData(lastSortColumn)
End Sub
**************************************************************
Here is my DataGrid
*********************************************************
<asp:DataGrid AllowPaging="true"
AllowCustomPaging="false"
PageSize="10"
PagerStyle-Visible="false"
Visible=false
AllowSorting="True"
AutoGenerateColumns="false"
CellPadding="3"
CellSpacing="0"
GridLines="Both" BorderColor="#666666"
ID="DataGrid1"
runat="server"
ShowFooter="false"
ShowHeader="true"
OnSortCommand="SortDataGrid"
BorderWidth="1"
style="margin:auto; width:700px">
<headerstyle HorizontalAlign="center" BackColor="#6ce0f7"
ForeColor="#3EA2BC" Font-Bold="true" />
<itemstyle BackColor="#F2F2F2" />
<alternatingitemstyle BackColor="#E5E5E5" />
<footerstyle HorizontalAlign="center" BackColor="#E8EBFD"
ForeColor="#3D3DB6" Font-Bold="true" />
<pagerstyle BackColor="white" />
<columns>
<asp:BoundColumn DataField="Applied"
ReadOnly="true"
Visible="True" ItemStyle-VerticalAlign="Top">
<itemstyle ForeColor="red" />
</asp:BoundColumn>
<asp:BoundColumn DataField="Rank"
HeaderText="Rank"
ReadOnly="true"
Visible="False" ItemStyle-VerticalAlign="Top"
SortExpression="Rank"> </asp:BoundColumn>
<asp:HyperLinkColumn DataTextField="JobTitle"
DataTextFormatString="{0}" DataNavigateUrlField="PositionID"
DataNavigateUrlFormatString="displayPositionOnly.aspx?PositionID={0}"
headertext="Job Title"
sortexpression="JobTitle" ItemStyle-VerticalAlign="Top"/>
<asp:BoundColumn DataField="Company"
HeaderText="Company"
ReadOnly="true"
Visible="True" ItemStyle-VerticalAlign="Top"
SortExpression="Company"/>
<asp:BoundColumn DataField="Location"
HeaderText="Location"
ReadOnly="true"
Visible="True" ItemStyle-VerticalAlign="Top"
SortExpression="Location"/>
<asp:BoundColumn DataField="Posted"
HeaderText="Date"
ReadOnly="true"
Visible="True" ItemStyle-VerticalAlign="Top"
SortExpression="DatePosted">
<itemstyle CssClass="datedisplay" />
</asp:BoundColumn>
<asp:BoundColumn DataField="lastUpdated"
HeaderText="Last Updated"
ReadOnly="true"
Visible="false" ItemStyle-VerticalAlign="Top"
SortExpression="p.DateUpdated">
<itemstyle CssClass="datedisplay" />
</asp:BoundColumn>
</columns>
</asp:DataGrid>
<asp:DataGrid AllowPaging="true"
AllowCustomPaging="false"
PageSize="4"
PagerStyle-Visible="false"
visible="false"
AllowSorting="True"
AutoGenerateColumns="false"
CellPadding="3"
CellSpacing="0"
GridLines="Both" BorderColor="#666666"
ID="DataGrid2"
runat="server"
ShowFooter="false"
ShowHeader="true"
OnSortCommand="SortDataGrid"
BorderWidth="1"
style="margin:auto;width:700px">
<HeaderStyle HorizontalAlign="center" BackColor="#6ce0f7"
ForeColor="#3EA2BC" Font-Bold="true" />
<ItemStyle BackColor="#F2F2F2" />
<AlternatingItemStyle BackColor="#E5E5E5" />
<FooterStyle HorizontalAlign="center" BackColor="#E8EBFD"
ForeColor="#3D3DB6" Font-Bold="true" />
<PagerStyle BackColor="white" />
<Columns>
<asp:BoundColumn DataField="Applied"
ReadOnly="true"
Visible="True" ItemStyle-VerticalAlign="Top">
<itemstyle ForeColor="red" />
</asp:BoundColumn>
<asp:BoundColumn DataField="Rank"
HeaderText="Rank"
ReadOnly="true"
Visible="False" ItemStyle-VerticalAlign="Top"
SortExpression="Rank">
</asp:BoundColumn>
<asp:TemplateColumn SortExpression="JobTitle"
ItemStyle-VerticalAlign="Top" HeaderText="Job Title">
<itemtemplate>
<asp:hyperlink text='<%# Container.DataItem("JobTitle")%>'
NavigateUrl='<%# "displayPositionOnly.aspx?PositionID=" &
Container.DataItem("PositionID") %>' runat="server" /><br>
<asp:label text='<%# Container.DataItem("JobDescription")%>'
runat="server" />...[<asp:hyperlink text='more' NavigateUrl='<%#
"displayPositionOnly.aspx?PositionID=" & Container.DataItem("PositionID")
%>' runat="server" />]
<br><asp:Label Text="Job Category: " style="font-weight:bold"
runat="server" /><asp:label text='<%# Container.DataItem("Category")%>'
runat="server" />
<br><asp:Label Text="Compensation: " style="font-weight:bold"
runat="server" /><asp:label text='<%# Container.DataItem("SalaryMin")%>'
runat="server" />
<asp:Label Text="- " style="font-weight:bold" runat="server"
/><asp:label text='<%# Container.DataItem("SalaryMax")%>' runat="server" />
</itemtemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Company"
HeaderText="Company"
ReadOnly="true"
Visible="True" ItemStyle-VerticalAlign="Top"
SortExpression="Company"/>
<asp:BoundColumn DataField="Location"
HeaderText="Location"
ReadOnly="true"
Visible="True" ItemStyle-VerticalAlign="Top"
SortExpression="Location"/>
<asp:BoundColumn DataField="Posted"
HeaderText="Date"
ReadOnly="true"
Visible="True" ItemStyle-VerticalAlign="Top"
SortExpression="DatePosted">
<Itemstyle CssClass="datedisplay" />
</asp:Boundcolumn>
<asp:BoundColumn DataField="lastUpdated"
HeaderText="Last Updated"
ReadOnly="true"
Visible="false" ItemStyle-VerticalAlign="Top"
SortExpression="p.DateUpdated">
<Itemstyle CssClass="datedisplay" />
</asp:Boundcolumn>
</Columns>
</asp:DataGrid>
************************************************************

Even if I change the CurrentPageIndex, is there something I need to do to
tell the page to reposition based on it?

Thanks,

Tom
 
S

Scott M.

Trust me, you do not need to go get your data on each page call, the data is
passed to the client and then back up to the server on each page call via
ViewState.

Try this: Run your page so that your grid is showing data. Now, view the
source code of the page that was delivered to your browser. See that large
amount of encoded crap at the top of the page? That is ALL of your database
data (as well as the state of your DataGrid and other Web Form controls you
are using on the page).

As for your code, I think that you are going about this all wrong.

This is a sample of how your Page_Load and DataGrid event handlers should
look:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not IsPostBack Then
'First time page is loading...
'Run whatever code you are using that goes to the data source and
gets you your data
'Would be best to have that returned data stored in a DataSet

'Bind our DataGrid to the DataSet
dg.DataSource = ds.Tables(0)
dg.DataBind()
Else
'Not the first time page is loading so no need to go back to the
data source
'We don't want to call the DataGrid's DataBind method here because
the
'reason the page is loading again is because user wanted to sort or
change pages
'or edit or update, etc.
' --- WE WILL BIND THE GRID IN THOSE SPECIFIC EVENT HANDLERS

'We may have sorted on a previous Page_Load so re-establish the sort
here
'If we hadn't sorted on a previouse Page_Load, this will just show
the unsorted data
If Not IsNothing(viewstate("prevSort")) Then
Dim dv As New DataView(ds.Tables(0))
dv.Sort = CType(viewstate.Item("prevSort"), String)
'Bind the grid to the DataView
dg.DataSource = dv
End If
End If
End Sub

Private Sub dg_PageIndexChanged(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles
dg.PageIndexChanged
'This fires when someone attempts to go to a different page of data
dg.CurrentPageIndex = e.NewPageIndex
dg.SelectedIndex = -1
dg.EditItemIndex = -1
dg.DataBind()
End Sub

Private Sub dg_SortCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
dg.SortCommand
'We need a DataView so that we can see the data in a different sequence
Dim dv As New DataView(ds.Tables(0))
dv.Sort = e.SortExpression

'Bind the grid to the DataView
dg.DataSource = dv
dg.DataBind()

'Store the sort for future page_load events
viewstate.Add("prevSort", e.SortExpression)
End Sub

Private Sub dg_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dg.EditCommand
'Tell the grid which row you want to see in edit mode.
'When working with a DataGrid, the term "Item" indicates a row
dg.EditItemIndex = e.Item.ItemIndex
dg.SelectedIndex = e.Item.ItemIndex
dg.DataBind() ' <--- NOTICE THAT THIS WILL RE-CONNECT THE GRID TO THE
DATA
End Sub

Private Sub dg_CancelCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dg.CancelCommand
'Get out of edit mode
dg.EditItemIndex = -1
dg.DataBind()
End Sub

Private Sub dg_SelectedIndexChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles dg.SelectedIndexChanged
'Get out of edit mode (if we had been editing another row)
dg.EditItemIndex = -1
dg.DataBind()
End Sub





tshad said:
Scott M. said:
First, yes you are bringing all the data into the grid when you use
paging (unless you are using custom paging), you are only showing one
page of data at a time, but you are, in fact bringing all the data down
to the grid.

All of the Web Form controls (DataGrid included) persist their own state
between page requests via ViewState. ViewState is a hidden form field
that contains the entire page's state encoded and compressed into this
string. After you go and get the data on the first page call, ALL of the
data is placed into the ViewState hidden form field. On the next page
call, the data is taken out of ViewState and placed back into the various
controls that had the data in the first place. This is why you only need
to go to the data on the first page call.

If you are using the Custom Paging feature, you are going to be only
retrieving one page of data at a time and so in this circumstance you
would have to go back to the database on each page call to get the next
set of records.


I'm not using the Custom Paging Feature, but I am calling my BindData
procedure to get the data each time. The following procedure is linked to
my first/next/previous/last links and sets the CurrentPageIndex
accordingly. The last line calls the BindData procedure and it works fine.
But if what you say is true, I am doing a lot of extra work (and slowing
the page down, I would assume). The problem is that if I comment out the
BindData routine the Datagrid comes back exactly the same as it did before
post.

**********************************************************
Sub PagerButtonClick(sender As Object, e As EventArgs)
trace.warn("in PagerButtonClick")
'used by external paging UI
Dim arg As String = sender.CommandArgument
Dim oGrid = DataGrid1

Select arg
Case "next": 'The next Button was Clicked
If (oGrid.CurrentPageIndex < (oGrid.PageCount - 1)) Then
oGrid.CurrentPageIndex += 1
End If

Case "prev": 'The prev button was clicked
If (oGrid.CurrentPageIndex > 0) Then
oGrid.CurrentPageIndex -= 1
End If

Case "last": 'The Last Page button was clicked
oGrid.CurrentPageIndex = (oGrid.PageCount - 1)

Case Else: 'The First Page button was clicked
oGrid.CurrentPageIndex = Convert.ToInt32(arg)
End Select

'Now, bind the data!
' BindData(lastSortColumn)
End Sub
**************************************************************
Here is my DataGrid
*********************************************************
<asp:DataGrid AllowPaging="true"
AllowCustomPaging="false"
PageSize="10"
PagerStyle-Visible="false"
Visible=false
AllowSorting="True"
AutoGenerateColumns="false"
CellPadding="3"
CellSpacing="0"
GridLines="Both" BorderColor="#666666"
ID="DataGrid1"
runat="server"
ShowFooter="false"
ShowHeader="true"
OnSortCommand="SortDataGrid"
BorderWidth="1"
style="margin:auto; width:700px">
<headerstyle HorizontalAlign="center" BackColor="#6ce0f7"
ForeColor="#3EA2BC" Font-Bold="true" />
<itemstyle BackColor="#F2F2F2" />
<alternatingitemstyle BackColor="#E5E5E5" />
<footerstyle HorizontalAlign="center" BackColor="#E8EBFD"
ForeColor="#3D3DB6" Font-Bold="true" />
<pagerstyle BackColor="white" />
<columns>
<asp:BoundColumn DataField="Applied"
ReadOnly="true"
Visible="True" ItemStyle-VerticalAlign="Top">
<itemstyle ForeColor="red" />
</asp:BoundColumn>
<asp:BoundColumn DataField="Rank"
HeaderText="Rank"
ReadOnly="true"
Visible="False" ItemStyle-VerticalAlign="Top"
SortExpression="Rank"> </asp:BoundColumn>
<asp:HyperLinkColumn DataTextField="JobTitle"
DataTextFormatString="{0}" DataNavigateUrlField="PositionID"
DataNavigateUrlFormatString="displayPositionOnly.aspx?PositionID={0}"
headertext="Job Title"
sortexpression="JobTitle" ItemStyle-VerticalAlign="Top"/>
<asp:BoundColumn DataField="Company"
HeaderText="Company"
ReadOnly="true"
Visible="True" ItemStyle-VerticalAlign="Top"
SortExpression="Company"/>
<asp:BoundColumn DataField="Location"
HeaderText="Location"
ReadOnly="true"
Visible="True" ItemStyle-VerticalAlign="Top"
SortExpression="Location"/>
<asp:BoundColumn DataField="Posted"
HeaderText="Date"
ReadOnly="true"
Visible="True" ItemStyle-VerticalAlign="Top"
SortExpression="DatePosted">
<itemstyle CssClass="datedisplay" />
</asp:BoundColumn>
<asp:BoundColumn DataField="lastUpdated"
HeaderText="Last Updated"
ReadOnly="true"
Visible="false" ItemStyle-VerticalAlign="Top"
SortExpression="p.DateUpdated">
<itemstyle CssClass="datedisplay" />
</asp:BoundColumn>
</columns>
</asp:DataGrid>
<asp:DataGrid AllowPaging="true"
AllowCustomPaging="false"
PageSize="4"
PagerStyle-Visible="false"
visible="false"
AllowSorting="True"
AutoGenerateColumns="false"
CellPadding="3"
CellSpacing="0"
GridLines="Both" BorderColor="#666666"
ID="DataGrid2"
runat="server"
ShowFooter="false"
ShowHeader="true"
OnSortCommand="SortDataGrid"
BorderWidth="1"
style="margin:auto;width:700px">
<HeaderStyle HorizontalAlign="center" BackColor="#6ce0f7"
ForeColor="#3EA2BC" Font-Bold="true" />
<ItemStyle BackColor="#F2F2F2" />
<AlternatingItemStyle BackColor="#E5E5E5" />
<FooterStyle HorizontalAlign="center" BackColor="#E8EBFD"
ForeColor="#3D3DB6" Font-Bold="true" />
<PagerStyle BackColor="white" />
<Columns>
<asp:BoundColumn DataField="Applied"
ReadOnly="true"
Visible="True" ItemStyle-VerticalAlign="Top">
<itemstyle ForeColor="red" />
</asp:BoundColumn>
<asp:BoundColumn DataField="Rank"
HeaderText="Rank"
ReadOnly="true"
Visible="False" ItemStyle-VerticalAlign="Top"
SortExpression="Rank">
</asp:BoundColumn>
<asp:TemplateColumn SortExpression="JobTitle"
ItemStyle-VerticalAlign="Top" HeaderText="Job Title">
<itemtemplate>
<asp:hyperlink text='<%# Container.DataItem("JobTitle")%>'
NavigateUrl='<%# "displayPositionOnly.aspx?PositionID=" &
Container.DataItem("PositionID") %>' runat="server" /><br>
<asp:label text='<%# Container.DataItem("JobDescription")%>'
runat="server" />...[<asp:hyperlink text='more' NavigateUrl='<%#
"displayPositionOnly.aspx?PositionID=" & Container.DataItem("PositionID")
%>' runat="server" />]
<br><asp:Label Text="Job Category: " style="font-weight:bold"
runat="server" /><asp:label text='<%# Container.DataItem("Category")%>'
runat="server" />
<br><asp:Label Text="Compensation: " style="font-weight:bold"
runat="server" /><asp:label text='<%# Container.DataItem("SalaryMin")%>'
runat="server" />
<asp:Label Text="- " style="font-weight:bold" runat="server"
/><asp:label text='<%# Container.DataItem("SalaryMax")%>' runat="server"
/>
</itemtemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Company"
HeaderText="Company"
ReadOnly="true"
Visible="True" ItemStyle-VerticalAlign="Top"
SortExpression="Company"/>
<asp:BoundColumn DataField="Location"
HeaderText="Location"
ReadOnly="true"
Visible="True" ItemStyle-VerticalAlign="Top"
SortExpression="Location"/>
<asp:BoundColumn DataField="Posted"
HeaderText="Date"
ReadOnly="true"
Visible="True" ItemStyle-VerticalAlign="Top"
SortExpression="DatePosted">
<Itemstyle CssClass="datedisplay" />
</asp:Boundcolumn>
<asp:BoundColumn DataField="lastUpdated"
HeaderText="Last Updated"
ReadOnly="true"
Visible="false" ItemStyle-VerticalAlign="Top"
SortExpression="p.DateUpdated">
<Itemstyle CssClass="datedisplay" />
</asp:Boundcolumn>
</Columns>
</asp:DataGrid>
************************************************************

Even if I change the CurrentPageIndex, is there something I need to do to
tell the page to reposition based on it?

Thanks,

Tom
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top