gridview custom paging C#

Posted on 2009-12-23
Last Modified: 2013-12-17
When I enable PageIndex="5" data did not show up. if I remove it; then it works. If I need to have custome no. of page to let say 5 do i have to use code behind. or how can the gridview work with custome paging. thanks!
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageIndex="5"

        AllowSorting="True" AutoGenerateColumns="False" 



            <asp:BoundField DataField="CategoryId" HeaderText="CategoryId" 

                ReadOnly="True" SortExpression="CategoryId" />

            <asp:BoundField DataField="CatName" HeaderText="CatName" 

                ReadOnly="True" SortExpression="CatName" />

            <asp:BoundField DataField="Description" HeaderText="Description" 

                ReadOnly="True" SortExpression="Description" />



    <asp:EntityDataSource ID="EntityDataSource1" runat="server" 

        ConnectionString="name=eSafetyEntities" DefaultContainerName="eSafetyEntities" 


        Select="it.[CategoryId], it.[CatName], it.[Description]" 

         OrderBy ="it.[CategoryId]" >

Open in new window

Question by:fm250
    LVL 8

    Accepted Solution

    You should use PageSize = 5 and not PageIndex for PageIndex will list the 5th page of data.
    (Also allow Paging = true)

    If you want to have custome paging size create listbox with values 5 , 10 , 20, 50 or whatever you need.
    Then in code behind on pageload event set the GridView.PageSize = Listbox.Value
    LVL 10

    Assisted Solution

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="5"
            AllowSorting="True" AutoGenerateColumns="False"

    LVL 10

    Author Comment

    How do I add next, prev pics? thanks
    LVL 29

    Expert Comment

    by:Kumaraswamy R
    LVL 8

    Expert Comment

    In your GridView assign properties see the properties window under paging or simply do it programatically.
    Have JPG which will act as link buttons.

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="ProductID" DataSourceID="SqlDataSource1">
                        <PagerSettings FirstPageImageUrl="First.jpg" LastPageImageUrl="Last.jpg" NextPageImageUrl="Next.jpg" PreviousPageImageUrl="Previous.jpg" />

    LVL 10

    Author Comment

    I have this setings, but it did not show no of pages in the middle. I need something like:
     Prev 2 3 4 5 6 7 Next
    is possible to do in the gridview or it needs programming.

    <pagersettings mode="NextPreviousFirstLast" 

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    Title # Comments Views Activity
    Gridview Edit Row 10 33
    ASP.NET MVC Edit ViewModel Data 6 24
    Parse CSS value with RegEx 2 28
    What is MicroStrategy.NET? 2 20
    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now