shay a

shay a

  • 2.1k
  • 70
  • 948

paging in gridview after filtering

Jan 16 2022 7:36 PM

Hi,

I have a gridview whitch is bind to sqlite source, before I filter the values by date the paging works fine.

when I filter the values I get result with 4 pages when I try the second page it goes back to all the values before the filtering.

I have seen some questions about the same problem on the web but no answer helped me.

this is how I load the values to the dataviewgrid:

protected void Grid2()
{
    string query2 = "Select * From SiteReport ORDER BY Date ASC";
    databaseObject.OpenConnection();
    SQLiteDataAdapter da2 = new SQLiteDataAdapter(query2, databaseObject.MyConnection);

    da2.Fill(ds2);

    GridView2.DataSource = ds2;
    GridView2.DataBind();
    databaseObject.CloseConnection();
}

and this is how I filter by dates:

protected void Button1_Click(object sender, EventArgs e)
{
    ViewState.Add("flag", true);
    string query3 = "SELECT * FROM SiteReport WHERE Date <= '" + DatePicker1.CalendarDate.ToString("yyyy-MM-dd 23:45:00") + "' AND Date >= '" + DatePicker2.CalendarDate.ToString("yyyy-MM-dd 22:45:00") + "'";
    SQLiteCommand myCommand3 = new SQLiteCommand(query3, databaseObject.MyConnection);
    databaseObject.OpenConnection();
    SQLiteDataAdapter dx = new SQLiteDataAdapter(query3, databaseObject.MyConnection);
    dx.Fill(ds3);
    GridView2.DataSource = ds3;
    GridView2.DataBind();
    databaseObject.CloseConnection();
}

this is how I tried the paging so far, but know I get blank gridview when I choose page 2:

protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    if (ViewState["flag"] != null)
    {
        if ((bool)ViewState["flag"] == true)
        {
            GridView2.PageIndex = e.NewPageIndex;
            GridView2.DataSource = ds3;
            GridView2.DataBind();
        }
    }
    else
    {
        GridView2.PageIndex = e.NewPageIndex;
        GridView2.DataSource = ds2;
        GridView2.DataBind();
    }
}

Thanks,

Shay.


Answers (4)