Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
75
Paging with UltraWinGrid
posted

I couldn't find a sample of paging for UltraWinGrid, so I've written one.

This sample simulate paging on client side only, meaning, all data is loaded on startup & displayed in pages.

The sample supports an UltraWinGrid that feeds from MS BindingSource. It also supports DataSource changes (Add\delete\update).

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using Infragistics.Win.UltraWinGrid;

using System.Collections;

namespace PagingSample

{

public partial class UltraGridWithPaging : Form

{

private UltraGrid dataGrid;

private BindingSource bindingSource;

private ToolStrip pagingToolbar;

private ToolStripButton btnFirstPage;

private ToolStripButton btnPreviousPage;

private ToolStripLabel lblPageNumber;

private ToolStripButton btnNextPage;

private ToolStripButton btnLastPage;

private int _currentPage = 1;

private const int PAGE_SIZE = 10;

public UltraGridWithPaging()

{

InitializeComponent();

dataGrid =
new UltraGrid();

dataGrid.InitializeRow += new Infragistics.Win.UltraWinGrid.InitializeRowEventHandler(DataGrid_InitializeRow);

dataGrid.Dock = DockStyle.Fill;

this.Controls.Add(dataGrid);

bindingSource = new BindingSource();bindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(BindingSource_ListChanged);

dataGrid.DataSource = bindingSource;

ArrayList dataSource = new ArrayList();

for (int i = 1; i <= 30; i++)

{

dataSource.Add(i);

}

dataGrid.DataSource = dataSource;

btnFirstPage =
new ToolStripButton("<<");

btnFirstPage.Click += new System.EventHandler(this.btnFirstPage_Click);

btnPreviousPage = new ToolStripButton("<");

btnPreviousPage.Click += new System.EventHandler(this.btnPreviousPage_Click);

lblPageNumber = new ToolStripLabel("0/0");

btnNextPage = new ToolStripButton(">");

btnNextPage.Click += new System.EventHandler(this.btnNextPage_Click);

btnLastPage = new ToolStripButton(">>");

btnLastPage.Click += new System.EventHandler(this.btnLastPage_Click);pagingToolbar = new ToolStrip(new ToolStripItem[ {

btnFirstPage, btnPreviousPage, lblPageNumber, btnNextPage, btnLastPage});

pagingToolbar.Dock = DockStyle.Bottom;

pagingToolbar.LayoutStyle = ToolStripLayoutStyle.Flow;

this.Controls.Add(pagingToolbar);

RefreshGrid();

RefreshButtons();

}

private void DataGrid_InitializeRow(object sender, InitializeRowEventArgs e)

{

// Set tow visibility according to selected page

if (PAGE_SIZE > 0)

{

if (e.Row.Index < PAGE_SIZE * (_currentPage - 1) ||

e.Row.Index > (PAGE_SIZE * _currentPage) - 1)

e.Row.Hidden =
true;

else

e.Row.Hidden = false;

}

}

 

private void BindingSource_ListChanged(object sender, ListChangedEventArgs e)

{

// Check if the current page still exists

if (PAGE_SIZE > 0)

{

if (dataGrid != null &&dataGrid.Rows != null &&

dataGrid.Rows.All.Length > 0)

{

UltraGridRow lastRow = (UltraGridRow)dataGrid.Rows.All.LastOrDefault();

int lastPages = (lastRow.Index / PAGE_SIZE) + 1;

if (_currentPage > lastPages)

_currentPage = lastPages;

}

RefreshGrid();

RefreshButtons();

}

}

private void btnFirstPage_Click(object sender, EventArgs e)

{

_currentPage = 1;

RefreshGrid();

RefreshButtons();

}

private void btnPreviousPage_Click(object sender, EventArgs e)

{

_currentPage--;

RefreshGrid();

RefreshButtons();

}

private void btnNextPage_Click(object sender, EventArgs e)

{

_currentPage++;

RefreshGrid();

RefreshButtons();

}

private void btnLastPage_Click(object sender, EventArgs e)

{

_currentPage = (((
UltraGridRow)dataGrid.Rows.All.Last()).Index / PAGE_SIZE) + 1;

RefreshGrid();

RefreshButtons();

}

private void RefreshGrid()

{

foreach (UltraGridRow row in dataGrid.Rows.All)

{

row.Refresh(
RefreshRow.FireInitializeRow);

}

}

private void RefreshButtons()

{

UltraGridRow lastRow = null;

if (dataGrid != null &&

dataGrid.Rows != null &&

dataGrid.Rows.All.Length > 0)

{

lastRow = (
UltraGridRow)dataGrid.Rows.All.LastOrDefault();

}

if (lastRow != null)

{

btnPreviousPage.Enabled = _currentPage > 1;

btnFirstPage.Enabled = btnPreviousPage.Enabled;

btnNextPage.Enabled = lastRow.Index > (PAGE_SIZE * _currentPage) - 1;

btnLastPage.Enabled = btnNextPage.Enabled;

lblPageNumber.Text = _currentPage.ToString() +
"/" + Convert.ToString((lastRow.Index / PAGE_SIZE) + 1);

if (!btnPreviousPage.Enabled && !btnNextPage.Enabled)

pagingToolbar.Visible = false;

else

pagingToolbar.Visible = true;

}

else

{

pagingToolbar.Visible =
false;

}

}

}

}