Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ASP.Net and C#

I have a GridView with paging. When I click a page number, the gridview disappears. If I run a method to display a different view and clear that view, then run another method to reverse the process - the new page is diaplayed.

ASP.NET
<asp:GridView ID="LastThirtyDaysGrid" runat="server" CssClass="expensesgrid" AutoGenerateColumns="false" AllowPaging="true" PageSize="10" OnPageIndexChanging="LastThirtyDaysReportGrid_PageIndexChanging" CellPadding="10" HorizontalAlign="Center"  />


C#
protected void LastThirtyDaysReportGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{

    LastThirtyDaysGrid.PageIndex = e.NewPageIndex;
    LastThirtyDaysGrid.DataBind();
}


What I have tried:

I added
ASP.NET
EnableSortingAndPagingCallbacks="true"
.
I wanted to refresh the page so called
C#
Page_Load(null, null);
which did not cause any change.
Posted
Updated 20-Dec-22 10:24am
Comments
Richard Deeming 20-Dec-22 4:12am    
Where is the data source set? If you're setting it via code, you'll need to set it every time you bind the grid.
Chinger 20-Dec-22 15:40pm    
reportGridView is a GridView variable. It had the value of MonthToDateGrid when it's being populated initially

Originally set:

var dataAdapter = new SqlDataAdapter(command);
var dataTable = new DataTable();
dataAdapter.Fill(dataTable);

reportGridview.DataSource = dataTable;
reportGridview.DataBind();
ReportHeader.Text = reportTitle;

Page Change:
protected void MonthToDateGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{

MonthToDateGrid.PageIndex = e.NewPageIndex;
MonthToDateGrid.DataBind();
}

1 solution

I think between both solutions it worked.

C#
protected void LastThirtyDaysReportGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GetThirtyDay(null, null);
    LastThirtyDaysGrid.PageIndex = e.NewPageIndex;
    LastThirtyDaysGrid.DataBind();
}


I added the first line of code within the function. It calls the function that originally sets the GridView's data.


C#
  protected void GetThirtyDay(object sender, EventArgs e)
  {
      GenerateReport("LastThirtyDaysExpenses", LastThirtyDaysGrid, "Last Thirty Days' Expenses");
  }



private void GenerateReport(string storedProcName, GridView reportGridview, string reportTitle)
  {
      ClearGridViews();

      try
      {
          using (var conn = new SqlConnection(connectionString))
          {
              var command = new SqlCommand();
              command.CommandText = storedProcName;
              command.Connection = conn;
              command.CommandType = CommandType.StoredProcedure;

              var dataAdapter = new SqlDataAdapter(command);
              var dataTable = new DataTable();
              dataAdapter.Fill(dataTable);

              reportGridview.DataSource = dataTable;
              reportGridview.DataBind();
              ReportHeader.Text = reportTitle;
          }
      }
      catch (Exception exp)
      {
          string help = exp.ToString();
      }
  }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900