GridView WinForms Paging

53 posts, 2 answers
  1. William
    William avatar
    1 posts
    Member since:
    Dec 2010

    Posted 22 Feb 2011 Link to this post

    I agree Paging is a necessity. I have gotten very to acquainted with the feature in Web Applications and just seems like something is wrong when I try to load data in a grid without it now. I don't do Windows applications very often but I wish it were available when I did develop one
  2. Stefan
    Admin
    Stefan avatar
    2890 posts

    Posted 24 Feb 2011 Link to this post

    Hello William, 

    Thank you for your feedback. You can add your vote for this feature request in the provided link in my previous post.
     
    Regards,
    Stefan
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 24 Feb 2011 Link to this post

    Hello guys,

    Please take a look at the following sample and let me know what you think. It's just a very basic example of paging using a command bar. Sorry that i didn't have time to make it cleaner and handle a few other details, like overflow and so on, but i hope this will put you on the right pat if you decide to use this:

    First, the custom control:
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    using Telerik.WinControls.UI;
     
    public partial class GridWithPagingControl : UserControl
    {
        #region Constants and Fields
     
        private object dataSource;
        private Dictionary<int, object> pagesDictionary;
        private Timer resizeTimer;
        private int currentPageNo;
     
        #endregion Constants and Fields
     
        #region Constructors and Destructors
     
        public GridWithPagingControl()
        {
            InitializeComponent();
            pagesDictionary = new Dictionary<int, object>();
            resizeTimer = new Timer();
            resizeTimer.Interval = 100;
            resizeTimer.Tick += new EventHandler(resizeTimer_Tick);
        }
     
        #endregion Constructors and Destructors
     
        #region Overrides
     
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }
     
        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            if (radGridView1.DataSource == null)
            {
                ClearPages();
                radCommandBar1.Visible = false;
                return;
            }
     
            resizeTimer.Enabled = false;
            resizeTimer.Enabled = true;
        }
     
        #endregion Overrides
     
        #region Properties
     
        public RadGridView GridView
        {
            get
            {
                return radGridView1;
            }
        }
     
        public object DataSource
        {
            get
            {
                return dataSource;
            }
            set
            {
                if (dataSource != value)
                {
                    dataSource = value;
     
                    if (dataSource as IList == null)
                    {
                        throw new ArgumentException("DataSource must be of IList type");
                    }
     
                    CreatePages();
                    SelectFirstPage();
                }
            }
        }
     
        public int CurrentPageNo
        {
            get
            {
                return currentPageNo;
            }
        }
     
        #endregion Properties
     
        #region Public Methods
     
        public bool SelectPage(int pageNo)
        {
            if (!pagesDictionary.ContainsKey(pageNo))
            {
                return false;
            }
     
            radGridView1.DataSource = pagesDictionary[pageNo];
            txtCurrentPage.Text = pageNo.ToString();
            currentPageNo = pageNo;
            var button = stripElementPages.Items.Where(b => b.Tag is int && ((int)b.Tag) == pageNo).FirstOrDefault() as CommandBarToggleButton;
            if (button != null)
            {
                button.ToggleState = Telerik.WinControls.Enumerations.ToggleState.On;
            }
     
            CheckNavigationButtonsState();
     
            return true;
        }
     
        public bool SelectFirstPage()
        {
            return SelectPage(pagesDictionary.Keys.FirstOrDefault());
        }
     
        public bool SelectLastPage()
        {
            return SelectPage(pagesDictionary.Keys.LastOrDefault());
        }
     
        public bool SelectNextPage()
        {
            return SelectPage(currentPageNo + 1);
        }
     
        public bool SelectPreviousPage()
        {
            return SelectPage(currentPageNo - 1);
        }
     
        #endregion Public Methods
     
        #region Event Handlers
     
        void resizeTimer_Tick(object sender, EventArgs e)
        {
            resizeTimer.Stop();
            radCommandBar1.Visible = true;
            CreatePages();
            SelectFirstPage();
        }
     
        void pageButton_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            var button = sender as CommandBarToggleButton;
     
            if (button.ToggleState == Telerik.WinControls.Enumerations.ToggleState.Off)
            {
                return;
            }
     
            var pageNo = -1;
            int.TryParse(button.Tag.ToString(), out pageNo);
            if (pageNo == -1)
            {
                return;
            }
     
            //button.ToggleState = args.ToggleState;
     
            var selectedbutton = stripElementPages.Items.Where(i =>
                i is CommandBarToggleButton &&
                ((CommandBarToggleButton)i).ToggleState == Telerik.WinControls.Enumerations.ToggleState.On && i != button).FirstOrDefault() as CommandBarToggleButton;
            if (selectedbutton != null)
            {
                selectedbutton.ToggleState = Telerik.WinControls.Enumerations.ToggleState.Off;
            }
     
            if (currentPageNo != pageNo)
            {
                SelectPage(pageNo);
            }
        }
     
        void pageButton_ToggleStateChanging(object sender, StateChangingEventArgs args)
        {
            args.Canceled = (stripElementPages.Items.Where(i =>
                i is CommandBarToggleButton &&
                ((CommandBarToggleButton)i).ToggleState == Telerik.WinControls.Enumerations.ToggleState.On &&
                args.NewValue == Telerik.WinControls.Enumerations.ToggleState.Off).ToArray().Length) == 1;
        }
     
        private void radGridView1_DataBindingComplete(object sender, Telerik.WinControls.UI.GridViewBindingCompleteEventArgs e)
        {
        }
     
        private void btnFirst_Click(object sender, EventArgs e)
        {
            SelectFirstPage();
        }
     
        private void btnPrevious_Click(object sender, EventArgs e)
        {
            SelectPreviousPage();
        }
     
        private void btnNext_Click(object sender, EventArgs e)
        {
            SelectNextPage();
        }
     
        private void btnLast_Click(object sender, EventArgs e)
        {
            SelectLastPage();
        }
     
        private void brnSelect_Click(object sender, EventArgs e)
        {
            var pageNo = -1;
            int.TryParse(txtCurrentPage.Text, out pageNo);
            if (pageNo == -1)
            {
                return;
            }
            SelectPage(pageNo);
        }
     
        #endregion Event Handlers
     
        #region Methods
     
        private void CheckNavigationButtonsState()
        {
            btnFirst.Enabled = currentPageNo != 1;
            btnPrevious.Enabled = btnFirst.Enabled;
            btnLast.Enabled = currentPageNo != pagesDictionary.Count;
            btnNext.Enabled = btnLast.Enabled;
        }
     
        private void CreatePages()
        {
            ClearPages();
            if (dataSource == null)
            {
                return;
            }
     
            radGridView1.DataSource = dataSource;
            radGridView1.LoadElementTree();
     
            var numberOfRowsPerPage = radGridView1.GridElement.VisualRows.Count(c => c.RowInfo is GridViewDataRowInfo) - 1;
     
            var list = dataSource as IList;
            pagesDictionary.Clear();
            IList pageSource = null;
            for (int i = 0; i < list.Count; i++)
            {
                var pageNo = i / numberOfRowsPerPage + 1;
                if (!pagesDictionary.ContainsKey(pageNo))
                {
                    pageSource = Activator.CreateInstance(dataSource.GetType()) as IList;
                    pagesDictionary.Add(pageNo, pageSource);
                    var pageButton = new CommandBarToggleButton();
                    stripElementPages.Items.Add(pageButton);
                    pageButton.ToggleStateChanging += new StateChangingEventHandler(pageButton_ToggleStateChanging);
                    pageButton.ToggleStateChanged += new StateChangedEventHandler(pageButton_ToggleStateChanged);
                    pageButton.Text = pageNo.ToString();
                    pageButton.Tag = pageNo;
                    pageButton.Image = null;
                    pageButton.DrawText = true;
                }
     
                pageSource = pagesDictionary[pageNo] as IList;
                pageSource.Add(list[i]);
            }
     
            lblNumPages.Text = pagesDictionary.Count.ToString();
     
            radCommandBar1.Visible = true;
        }
     
        private void ClearPages()
        {
            var label = stripElementPages.Items.FirstOrDefault();
     
            stripElementPages.Items.Clear();
            if (label != null)
            {
                stripElementPages.Items.Add(label);
            }
        }
     
        #endregion Methods
    }

    and the test form:
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Forms;
     
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Size = new Size(640, 480);
        }
     
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            LoadListDataSource();
        }
     
        private void LoadListDataSource()
        {
            var employees = new List<Employee>();
            for (int i = 0; i < 100; i++)
            {
                employees.Add(new Employee
                {
                    Id = i,
                    Name = "Employee " + i,
                    Email = "Employee." + i + "@something.com"
                });
            }
     
            gridWithPagingControl1.GridView.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
            gridWithPagingControl1.DataSource = employees;
        }
    }
     
    public class Employee
    {
        public int Id
        {
            get;
            set;
        }
     
        public string Name
        {
            get;
            set;
        }
     
        public string Email
        {
            get;
            set;
        }
    }

    If you have any problems just let me know, meanwhile i hope i'll have some time to clean up a few of the details and add this to the code library.

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga
    Telerik WinForms MVP
  5. Stefan
    Admin
    Stefan avatar
    2890 posts

    Posted 25 Feb 2011 Link to this post

    Hi Emanuel,

    Thank you for providing this sample of RadGridView paging functionality. Since it seems quite interesting, I think it is a good idea to make a Code Library article based on it. This will allow our users to find it easily.

    Thank you for your cooperation.

    All the best,
    Stefan
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  6. Jaco Van Zyl
    Jaco Van Zyl avatar
    4 posts
    Member since:
    May 2006

    Posted 25 Feb 2011 Link to this post

    Hi Emanuel

    Could you perhaps upload the code here.  It will just make it slightly easier than creating the usercontrol (ie commandbar with buttons etc).

    Yip, I can be very lazy sometimes...

    Jaco van Zyl
  7. Nikolay
    Admin
    Nikolay avatar
    1804 posts

    Posted 25 Feb 2011 Link to this post

    Hello Jaco Van Zyl,

    Our forums does not allow for attaching files. One should post a code library article if he/she wants to share his/her solution with the community.

    All the best,
    Nikolay
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  8. Jaco Van Zyl
    Jaco Van Zyl avatar
    4 posts
    Member since:
    May 2006

    Posted 25 Feb 2011 Link to this post

    Thanks for the info Nikolay.
  9. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 25 Feb 2011 Link to this post

    Hello again guys,

    Sorry again for the late reply but everything is crazy lately.

    I have just submitted the project to the code library with a few enhancements, like support for sorting, filtering.

    Please take a look at the attached images for more info.
    As soon as the project will be approved i will post a link here.

    On a sadder note (maybe), this version will only support IList data sources.

    Please feel free to suggest other functionalities or other possible behaviors.

    Best Regards,
    Emanuel Varga
    Telerik WinForms MVP
  10. Stefan
    Admin
    Stefan avatar
    2890 posts

    Posted 28 Feb 2011 Link to this post

    Hi guys,

    Thanks to Emanuel there is a sample application demonstrating this funcionality. It is available in our Code Library section at this link

    I hope you will find this useful.

    Greetings,
    Stefan
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  11. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 01 Mar 2011 Link to this post

    Hello guys,

    So, did someone have any time to take a look at the grid with paging code project?

    Best Regards,
    Emanuel Varga
    Telerik WinForms MVP
  12. &lt;&gt;&lt;
    &lt;&gt;&lt; avatar
    9 posts
    Member since:
    Apr 2011

    Posted 19 Apr 2011 Link to this post

    Please note, I am not a telerik user yet, but will be starting using your controls at work in the coming weeks and am reading up on it.

    By just glancing at the code, it seems that the entire datasource is loaded and then all the paging etc is done, which I believe will cause the application to crawl if a significant amount of rows are loaded...and there is no need to load 1million rows to be able to use the filtering capabilities.

    Instead, if you can handle the filtering manually (ie, no automatic filtering and handle events triggered before filtering is going to take place) and convert that telerik filter conditions into a equivalent SQL query, this can be handled very easily. 

    I don't know if the telerik controls have the ability to convert telerik grid filter conditions to SQL server queries, if not, you can probably create a recursive function to create a SQL query from the filter condition (assuming you have access to the filter conditions)...the ability for telerik to automatically convert it to a SQL query would be a plus.

    Everything else can be handled by code very easily, as I have done that using another grid by another company and it works really well, as I only the top 500 rows and allow the user paginate through them.  When the grid is filtered, a new query is created and the returned value is then bound to the grid....etc.

    Just my 2c

    Take Care
  13. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 22 Apr 2011 Link to this post

    Hi Karim,

    Thank you for your comments and suggestions. Currently, this functionality is not natively supported by RadGridView. The desired behavior can be achieved by using the custom filtering and converting FilterDescriptors collection to SQL query. 

    We will continue researching the possible implementation in Windows Forms context and binding. This feature will be included in one of the next major releases of RadControls for Winforms.

    Best wishes,
    Julian Benkov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  14. Lizeth
    Lizeth avatar
    1 posts
    Member since:
    Jul 2011

    Posted 04 Aug 2011 Link to this post

    Me too vote for paging, I want to buy the controls and this case is very importat for the company where it going to implement. =S
  15. Svett
    Admin
    Svett avatar
    728 posts

    Posted 08 Aug 2011 Link to this post

    Hello Lizeth,

    Thank you for writing.

    The number of votes for this feature increase its priority in our TO DO list. Feel free to vote for it here.

    Greetings,
    Svett
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

  16. h g
    h g avatar
    11 posts
    Member since:
    Apr 2010

    Posted 12 May 2012 Link to this post

    Did you still implement this feature? if not when is your estimate ?
  17. Svett
    Admin
    Svett avatar
    728 posts

    Posted 15 May 2012 Link to this post

    Hi h g,

    This feature is not implemented yet. In order to increase its priority, you can vote for it in at link that I have already provided in my previous post.

    Kind regards,
    Svett
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
  18. Stanley
    Stanley avatar
    32 posts
    Member since:
    Apr 2012

    Posted 24 Jun 2012 Link to this post

    Found the link, 48 voted
    http://www.telerik.com/support/pits.aspx#/public/winforms/4775

    How come 5 years still does not provide the Paging, very very disappointed.

  19. Svett
    Admin
    Svett avatar
    728 posts

    Posted 25 Jun 2012 Link to this post

    Hi Stanley,

    The paging is more suitable for web applications and it is not a common UI in desktop application. Moreover, this features is covered by a pretty well working Code Library article. Therefore, this feature is postponed in favor of other desired WinForms features that would be hard to be implemented outside the WinForms suite.

    Kind regards,
    Svett
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
  20. Jati
    Jati avatar
    1 posts
    Member since:
    Jun 2012

    Posted 22 Jul 2012 Link to this post

    Wow...it's been 5 years in queue
  21. Robert LeGood
    Robert LeGood avatar
    28 posts
    Member since:
    Dec 2007

    Posted 18 Apr 2013 Link to this post

    Man, I'd love to have this functionality.    It would help my current app that needs to display >600,000 rows of data.

    The problem with the user supplied solution is that is needs objects that are ILists, and I only have DataTables.  In addition, I imagine that filter boxes would only read from the visible values.  (vs. all the values when creating the Excel like filtering options)

    Rob.
  22. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 22 Apr 2013 Link to this post

    Hello Rob,

    I am sorry for the inconvenience caused.

    As we mentioned paging is not a common for UI in desktop application. You can change our example to use a DataTable as follows:
    private void LoadListDataSource()
           {
              var employees = new List<Employee>();
              int i = 0;
              foreach(DataRow dr in yourTable.Rows)
               {
                   Employee emp = new Employee();
                   emp.Id = (int)dr["ID"].ToString();
                   emp.Name = "Employee " +dr["Name"].ToString();
                   employees.Add(emp);
                   ++i;
               }

    You can refer to this StackOverflow article for more details about convert DataTable to List.

    I hope this helps.

    Kind regards,
    Peter
    the Telerik team
    WinForms Q1 2013 boasts PivotGrid, PDF Viewer, Chart enhancements and more. Check out all of the latest highlights.
  23. Alvaro
    Alvaro avatar
    1 posts
    Member since:
    Oct 2013

    Posted 14 Oct 2013 Link to this post

    Hello Everyone,

    please, sorry for my English.

    There is a bug with the Paging Example, when yo try to filtering data using the "Custom Filtering".
    It shows a Exception Null Reference in class Main for the GridWithPaging example.
    I want to know if there is a solution for this bug.

    thank's

    Alvaro.
  24. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 16 Oct 2013 Link to this post

    Hi Alvaro,

    Thank you for writing.

    The provided information is not enough to determine what causes this. Could you, please, enable the exceptions in your Visual Studio (Debug -> Exceptions -> Managed Exceptions, check the checkbox) in order to debug where exactly the project crashes and let me know what is the result on your side?

    Please, have in mind that the filtering is not supported in this mode and can be achieved by using the custom filtering but you should manually convert the FilterDescriptors collection to SQL query.

    I hope this helps.

    Regards,
    Peter
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
Back to Top
UI for WinForms is Visual Studio 2017 Ready