How to Do Pagination in Datagridview in Winform

How can we do pagination in datagridview in winform

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;
}
}
}
}

Datagridview pagination in C# windows application

Sure, this is a very frequently asked question. Just look at this and this.

paging techniques for datagridview using in winforms applications

Just tried to check if Google works correctly :)

Paging in DataGrid Winforms

How to paging a datagridview in winform?

A Simple Way for Paging in DataGridView in WinForm Applications

Hope this articles will be helpful for you.

DataGridView paging

Should be easy to do .
Just edit the properties of the datagrid in designview(little arrow at the right) and enable it. Have a look at this aticle

gridview search and paging in C#

There's a simple answer by Rick Mohr on how to do this using BindingNavigator and BindingSource.

You may also want to look at some other articles/implementations.

  • DataGridView With Paging Step by Step
  • Paging in DataGridView
  • A simple way for Paging in DataGridView
  • DataGrid Paging

Adding pagination to a DataGridView

As you may know, pagination isn't an inherint element to the windows forms datagridview. However, with the bindingnavigator and binding source, you can achieve the results you are looking for.
I have researched this solution, that I found in C#. However, you can easily convert the code to vb.net. I instruct you to the original document so that you can follow and understand the concept.
Sorry, but pagination can not be added to your code - much is needed to achieve your results.

Custom Datagridview Paging

after working on it I was able to fix the problem, here is the working class:

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)
{
if (dataTable == null || bnav == null)
{
return;
}

DataTable dt = null;
bs = new BindingSource();
tables = new BindingList<DataTable>();
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)
{
try
{
this.DataSource = tables[bs.Position];
}
catch (Exception ex)
{

MessageBox.Show(ex.Message);
}
}


Related Topics



Leave a reply



Submit