Click here to Skip to main content
16,022,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The paging index is not displayed when rebinding to search results making it impossible for user to see all the search results in the rest of the pages.

What I have tried:

Below is the code i use to load data when it has been filtered
C#
private void BindGrid(string searchTerm = "")
{
    var data = GetData();
    int totalEntries = data.Count;

    if (!string.IsNullOrWhiteSpace(searchTerm))
    {
        data = data.Where(d => d.GetType().GetProperties()
            .Any(prop => prop.GetValue(d, null)?.ToString().IndexOf(searchTerm, StringComparison.OrdinalIgnoreCase) >= 0)).ToList();
    }

    int filteredEntries = data.Count;
    int entriesPerPage = GridView1.PageSize;

    GridView1.AllowPaging = true;

    // Adjust PageIndex if it exceeds the number of available pages
    int totalPages = (int)Math.Ceiling((double)filteredEntries / entriesPerPage);
    if (GridView1.PageIndex >= totalPages)
    {
        GridView1.PageIndex = totalPages - 1; // Set to last page if current exceeds total
    }

    // Ensure PageIndex is reset if no results are found
    if (filteredEntries == 0)
    {
        GridView1.PageIndex = 0;
    }

    GridView1.DataSource = data.Skip(GridView1.PageIndex * entriesPerPage).Take(entriesPerPage).ToList();
    GridView1.DataBind();

    LabelEntryCount.Text = $"Showing {GridView1.Rows.Count} of {filteredEntries} entries";

    GridView1.Visible = filteredEntries > 0;
    GridView1.PagerSettings.Visible = filteredEntries > entriesPerPage;
}
The below is what i use to load data when its not filtered
C#
private void BindGrid()
{
    var data = GetData();
    int totalEntries = data.Count;

    GridView1.DataSource = GetData();
    GridView1.DataBind();

    int currentPageEntries = GridView1.Rows.Count;
    LabelEntryCount.Text = "Showing " + currentPageEntries + " of " + totalEntries + " entries";

}
Posted
Updated 6-Sep-24 1:53am
v2

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