DataGrid Disappears After Paging

L

Leonard

I am using a datagrid that is bound to a dataset that
contains 18 records. The page size is 6 rows. On the
page_load the first six rows are displayed as expected.

The grid allows default paging and I have a page index
changed handler. In the handler I see the
CurrentPageIndex increment when I press the "Next" button
but the entire data grid disappears when the page is
displayed again.

What has happened? How do I fix it?

Thanks!
 
S

Stevie_mac

You gotta call DataGrid.Bind()

I am assuming in Page_Load() you are doing...
If IsPostBack = False then
'Get Data, Set DataGrid.Source & DataGrid.Bind()
End if

so when the page reloads (following a Next click), the datagrid is not bound
again.

try...

Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) _
Handles DataGrid1.PageIndexChanged
DataGrid1.CurrentPageIndex = e.NewPageIndex
DataGrid1.DataBind()
End Sub
 
L

Leonard

The issue, it seems, is not rebinding the datagrid but
supplying it with a data source in the page changed
handler. In looking at the DataSource property of the
data grid in the debugger in the page_load after the
DataBind() the it has a value set to a DataSet. However,
in the page changed handler after resetting the
CurrentPageIndex the DataSource property IS NOT defined.
However, after coding the handler as follows things seem
to work.

private void dgInterests_PageIndexChng(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
strQuery = "SELECT ...";
SqlDataAdapter adObject = new SqlDataAdapter(strQuery,
connObject); // Connection must be instantiated
// and opened first
DataSet ds = new DataSet();
adObject.Fill(ds);

dg.DataSource = ds; // dg is DataGrid instantiated
elsewhere
dg.CurrentPageIndex = e.NewPageIndex;
dg.DataBind();
}
 
S

Stevie_mac

Oops, did i miss that out - well, at least you sussed it! Yes, you do have to
supply the source again before rebinding.!

What i tend to do is - have a function called BindGrid. Call BindGrid() in
Page_Load() on 1st load only (ie test for Not IsPostBack) then call BindGrid()
(in the respective event handler) after paging / row selection / Row deletion
etc.
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top