Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we do pagination in datagridview in winform

Tags:

I want to show 10 records per page in a datagridview on a window form and user must click next button to show next 10 records. Is it there some property in DataGridview or do i need to create a custom control.

What i need to do to achieve this.

like image 824
Shantanu Gupta Avatar asked May 13 '10 09:05

Shantanu Gupta


People also ask

How many rows can DataGridView handle?

Max value, i.e. 2,147,483,647. The DataGridView's RowCount cannot hold a number more than this because it's an integer. So, you can possibly say that limit for datagridview rows is 2 Billion.


2 Answers

Here's a simple working example, where a BindingNavigator GUI control uses a BindingSource object to identify page breaks, by setting its DataSource to a custom subclass of IListSource. (Thanks to this answer for the key idea.) When the user clicks the "next page" button, the BindingNavigator fires bindingSource1_CurrentChanged and your code can fetch the desired records. Instructions:

  1. Create a Windows Forms application
  2. Drag onto the form a BindingNavigator, a DataGridView, and a BindingSource
  3. Replace Form1.cs with the following code:
using System; using System.Collections.Generic; using System.Windows.Forms;  namespace PagedDataGridView {     public partial class Form1 : Form     {         private const int totalRecords = 43;         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)         {             // The desired page has changed, so 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;             }         }     } } 
like image 136
Rick Mohr Avatar answered Oct 27 '22 16:10

Rick Mohr


Here is my solution : It took me almost a year to find it and proud of this one

public class SuperGrid : DataGridView     {         public int PageSize         {             get             {                 return _pageSize;             }             set             {                 _pageSize = value;             }         }         public int _pageSize = 10;         BindingSource bs = new BindingSource();         BindingList<DataTable> tables = new BindingList<DataTable>();         public void SetPagedDataSource(DataTable dataTable, BindingNavigator bnav)         {             DataTable dt = null;             int counter = 1;             foreach (DataRow dr in dataTable.Rows)             {                 if (counter == 1)                 {                     dt = dataTable.Clone();                     tables.Add(dt);                 }                 dt.Rows.Add(dr.ItemArray);                 if (PageSize < ++counter  )                 {                     counter = 1;                 }             }             bnav.BindingSource = bs;             bs.DataSource = tables;             bs.PositionChanged += bs_PositionChanged;             bs_PositionChanged(bs, EventArgs.Empty);         }         void bs_PositionChanged(object sender, EventArgs e)         {             this.DataSource = tables[bs.Position];         }     } 

How to use it? Add above code to your project, drag the Supergrid and a bindingnavigator control to your win form .

 superGrid1.PageSize = 5;  DataTable dt = DataProvider.ExecuteDt("select * from test order by col");   superGrid1.SetPagedDataSource(dt, bindingNavigator1); 

And you get a paged Datagridview with data binding without much hastle/

like image 24
Thunder Avatar answered Oct 27 '22 15:10

Thunder