I have set my Gridview to allow paging.
try
{
SqlConnection sqlConnection = new SqlConnection("Data Source=JACKCONNECTION\\SQLEXPRESS;Initial Catalog=testbase;Integrated Security=True");
SqlCommand sqlCommand = new SqlCommand(allitemsselectedsqlsrc, sqlConnection);
sqlCommand.CommandType = System.Data.CommandType.Text;
sqlConnection.Open();
SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
DataSet ds = new DataSet();
da.Fill(ds);
ArrayList ArrList = new ArrayList();
foreach (DataRow dr in ds.Tables[0].Rows)
{
ArrList.Add(dr);
}
GridViewMass.DataSource = ds;
GridViewMass.DataBind();
}
catch (Exception err)
{
LabelSelErr.Text = err.Message;
}
Also, I have a action PageIndexChanging for GridView as follows:
protected void gvm_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridViewMass.PageIndex = e.NewPageIndex;
GridViewMass.DataBind();
}
Lastly, the aspx file contain the Gridview is as follows:
<asp:GridView ID="GridViewMass" runat="server" AllowPaging="True"
AllowSorting="True" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None"
BorderWidth="1px" CellPadding="4" EnableSortingAndPagingCallbacks="True"
ForeColor="Black" GridLines="Horizontal"
onpageindexchanging="gvm_PageIndexChanging">
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#4B4B4B" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#242121" />
</asp:GridView>
The strange thing happen is when I click on the page (be it the 2nd page, 3rd page or whatever page that I could click), the Gridview GridViewMass will just disappear.
Did I code wrongly? Previously, I encountered the following error messages and have solved them but right now, I end up with something that I could not continue anymore.
- A first chance exception of type ‘System.InvalidOperationException’ occurred in System.dll
- there is already an open datareader associated with this command which must be closed first.
- The data source does not support server-side data paging.
- The GridView ‘GridView’ fired event PageIndexChanging which wasn’t handled.
Appreciate any help that can help me get my GridView back.
You are missing just one thing in your
PageIndexChangingeventIn your Try/catch block you have to store that dataset in
ViewStateNow in your
pageIndexChangeingchange like thisAlso remember to set your
EnableSortingAndPagingCallbackstoFalse!