toosensitive
toosensitive

Reputation: 2375

Windows Forms GridView Paging

I have a grid view (windows forms) on my application. The data is from web service call. Sometimes the data is huge and it takes a while. So I want to add some feature like page. So users can click first page, previous page, next page, last page. I know in asp.net there is such control. Wonder for windows forms, is there a similar control available? If not I have to code up myself thanks

Upvotes: 1

Views: 3150

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222682

When the user clicks the "next page" button, in event bindingSource1_CurrentChanged and your code can bind the records

Drag onto the form a BindingNavigator, a DataGridView, and a BindingSource

namespace PagedView
{
    public partial class Form1 : Form
    {
        private const int totalRecords = 40;
        private const int pageSize = 10;

        public Form1()
        {
            InitializeComponent();
            dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { DataPropertyName = "Index" });
            bindingNavigator1.BindingSource = bindingSource1;
            bindingSource1.CurrentChanged += new System.EventHandler(bindingSource1_CurrentChanged);
            bindingSource1.DataSource = new PageOffsetList();
        }

        private void bindingSource1_CurrentChanged(object sender, EventArgs e)
        {
            // fetch the page of records using the "Current" offset 
            int offset = (int)bindingSource1.Current;
            var records = new List<Record>();
            for (int i = offset; i < offset + pageSize && i < totalRecords; i++)
                records.Add(new Record { Index = i });
            dataGridView1.DataSource = records;
        }

        class Record
        {
            public int Index { get; set; }
        }

        class PageOffsetList : System.ComponentModel.IListSource
        {
            public bool ContainsListCollection { get; protected set; }

            public System.Collections.IList GetList()
            {
                // Return a list of page offsets based on "totalRecords" and "pageSize"
                var pageOffsets = new List<int>();
                for (int offset = 0; offset < totalRecords; offset += pageSize)
                    pageOffsets.Add(offset);
                return pageOffsets;
            }
        }
    }
}

or follow this link

Paging in Winform

Upvotes: 1

Related Questions