Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i am trying to create a gridview how will hold big data i am using a checkbox to select he rows but i am getting rows only from first page of gridivew how to fix it.

What I have tried:

I have tried this.

<pre lang="ASP.NET"><pre><asp:GridView ID="datatable" Width="100%" runat="server" AutoGenerateColumns="False"
                                    CssClass="table table-striped jambo_table table-bordered grid-table sorted_table department-grid FixedHeader"
                                    OnRowCommand="datatable_RowCommand" OnRowDeleting="datatable_RowDeleting"
                                    OnRowDataBound="datatable_RowDataBound" OnPreRender="datatable_PreRender"
                                    ClientIDMode="Static" CellPadding="4" ForeColor="#333333"
                                    PageSize="50" GridLines="None" EmptyDataText="There is no product exists"
                                    ShowHeaderWhenEmpty="True">
                                    <Columns>
                                        <%--0--%>
                                        <asp:TemplateField ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle">
                                            <HeaderTemplate>
                                                <asp:CheckBox ID="chkheader" runat="server" Text="All" AutoPostBack="true" OnCheckedChanged="sellectAll" />
                                            </HeaderTemplate>
                                            <ItemTemplate>
                                                <asp:CheckBox runat="server" ID="chkSelect"></asp:CheckBox>
                                            </ItemTemplate>
                                        </asp:TemplateField>  </Columns>
                                </asp:GridView>   




C#
<pre>  string PIDs = "";
            foreach (GridViewRow row in datatable.Rows)
            {
                CheckBox check = (CheckBox)row.FindControl("chkSelect");
                if (check.Checked)
                {
                    Label lbl_productid = (Label)row.FindControl("lbl_productid");
                    String PID = lbl_productid.Text.ToString();
                    if (PIDs.Length < 2)
                        PIDs = PID;
                    else
                        PIDs = PIDs + "," + PID;
                }
Posted
Updated 27-Jun-21 22:19pm
v2

1 solution

When you switch to a different page, all of the controls for the current page are destroyed. The GridView does not maintain any state for pages which are not displayed.

You will need to handle the PageIndexChanging event[^], extract the selected rows from the current page, and store them somewhere. Then, when you want to get all selected rows, retrieve the stored selection from the other pages and combine it with the selected rows on the current page.

For example:
C#
private Dictionary<int, List<string>> PageSelections
{
    get
    {
        var value = (Dictionary<int, List<string>>)Session["SelectedPIDs"];
        if (value == null)
        {
            value = new Dictionary<int, List<string>>();
            Session["SelectedPIDs"] = value;
        }
        
        return value;
    }
}

private List<string> ExtractSelectedRows()
{
    var result = new List<string>();
    foreach (GridViewRow row in datatable.Rows)
    {
        if (row.RowType != DataControlRowType.DataRow) continue;
        
        CheckBox check = (CheckBox)row.FindControl("chkSelect");
        if (!check.IsSelected) continue;
        
        Label lbl_productid = (Label)row.FindControl("lbl_productid");
        result.Add(lbl_productid.Text);
    }
    
    return result;
}

protected void datatable_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    // Save the selected rows before moving to the new page:
    PageSelections[datatable.PageIndex] = ExtractSelectedRows();
}

protected void datatable_DataBound(object sender, EventArgs e)
{
    // Restore the selected rows when a new page is displayed:
    List<string> selectedRows;
    if (!PageSelections.TryGetValue(datatable.PageIndex, out selectedRows)) return;
    if (selectedRows.Count == 0) return;
    
    foreach (GridViewRow row in datatable.Rows)
    {
        if (row.RowType != DataControlRowType.DataRow) continue;
        
        CheckBox check = (CheckBox)row.FindControl("chkSelect");
        Label lbl_productid = (Label)row.FindControl("lbl_productid");
        check.IsSelected = selectedRows.Contains(lbl_productid.Text);
    }
}

private IEnumerable<string> GetAllSelectedRows()
{
    // Save the selections from the current page:
    Dictionary<int, List<string>> pageSelections = PageSelections;
    pageSelections[datatable.PageIndex] = ExtractSelectedRows();
    
    // Return the PID from all selected rows:
    return pageSelections.SelectMany(pair => pair.Value);
}
 
Share this answer
 
Comments
Member 15001218 28-Jun-21 4:35am    
As you can see i am not using the changepageindex method in my gridview i am using default paging in which all the data loads once and creates paging after that change of page of gridview does not reloads or even refresh the data so states remain same.
Richard Deeming 28-Jun-21 4:37am    
And as I said, the GridView does not maintain state for pages which are not displayed.

You need to handle the PageIndexChanging event to extract the selected rows for the current page before the new page is displayed.
Member 15001218 13-Jul-21 22:53pm    
it did not work because i am jquerry datatable plugin for sorting and paging of the gridview .
Member 15001218 28-Jun-21 4:44am    
okay let me implement that.

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