locked
Different Page size for every page on gridview RRS feed

  • Question

  • User1553103031 posted

    Hi,  I´m using a gridview with  paging and I´d like to know If there´s a way to  have different page sizes for every page. I trie changing it at runtime , but when I change page It duplicates me some rows from previous page.

    Friday, October 18, 2019 5:31 PM

All replies

  • User288213138 posted

    Hi cesarmzzz,

    Hi,  I´m using a gridview with  paging and I´d like to know If there´s a way to  have different page sizes for every page. I trie changing it at runtime , but when I change page It duplicates me some rows from previous page.

    According to your description, I made demo for you.

    I create a dropdownlist control in RowCreated event, then I change the pagesize according to the value by the dropdownlist.

     <asp:GridView ID="grd1" runat="server" AutoGenerateColumns="false" AllowPaging="true" AllowSorting="true"
                OnPageIndexChanging="grd1_PageIndexChanging" OnRowCreated="grd1_RowCreated">
                <Columns>
                    <asp:TemplateField>
                        <HeaderTemplate>No</HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label ID="srno" runat="server" Text='<%# Container.DataItemIndex +1 %>' />
                        </ItemTemplate>
                    </asp:TemplateField>
    
    
                    <asp:TemplateField>
                        <HeaderTemplate>Country</HeaderTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtrollno" runat="server" Text='<%# Bind("Country") %>' />
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblrollno" runat="server" Text='<%# Bind("Country") %>' />
                        </ItemTemplate>
                    </asp:TemplateField>
    
                    <asp:TemplateField>
                        <HeaderTemplate>Name</HeaderTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtname" runat="server" Text='<%# Bind("Name") %>' />
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblname" runat="server" Text='<%# Bind("Name") %>' />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
    
     string strcon = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            static DataSet ds;
            SqlDataAdapter da;
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    bind();
                }
            }
            public void bind()
            {
                da = new SqlDataAdapter("select * from Customer", strcon);
                ds = new DataSet();
                da.Fill(ds);
                grd1.DataSource = ds.Tables[0];
                grd1.DataBind();
            }
            protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
            {
    
                grd1.PageSize = int.Parse(((DropDownList)sender).SelectedValue);
                grd1.DataSource = ds.Tables[0];
                grd1.DataBind();
            }
            protected void grd1_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                grd1.PageIndex = e.NewPageIndex;
                grd1.DataSource = ds.Tables[0];
                grd1.DataBind();
            }
            protected void grd1_RowCreated(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.Pager)
                {
                    DropDownList ddl = new DropDownList();
                    ddl.Items.Add("2");
                    ddl.Items.Add("3");
                    ddl.Items.Add("4");
                    ddl.Items.Add("5");
                    ddl.AutoPostBack = true;
                    ListItem li = ddl.Items.FindByText(grd1.PageSize.ToString());
                    if (li != null)
                    {
                        ddl.SelectedIndex = ddl.Items.IndexOf(li);
                    }
                    ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
                    Table pagerTable = e.Row.Cells[0].Controls[0] as Table;
                    TableCell cell = new TableCell();
               
                    cell.Controls.Add(ddl);
                    pagerTable.Rows[0].Cells.Add(cell);
                }
            }
            void ddl_SelectedIndexChanged(object sender, EventArgs e)
            {
                grd1.PageSize = int.Parse(((DropDownList)sender).SelectedValue);
                grd1.DataSource = ds.Tables[0];
                grd1.DataBind();
            }

    The result:

    Best regards,

    Sam

    Monday, October 21, 2019 7:08 AM