1. Giới thiệu
Xin chào các bạn hôm nay mình sẽ demo cho các bạn chương trình phân trang trên GridControl nhé 😀
2. Code
Tạo Form Main
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using DevExpress.Data;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DxSample.Filtering;
namespace DxSample {
public partial class MainForm : XtraForm {
private SqlDataAdapter DataAdapter;
public MainForm() {
InitializeComponent();
OrdersGridView.OptionsCustomization.AllowFilter = false;
OrdersGridView.OptionsCustomization.AllowGroup = false;
OrdersGridView.OptionsCustomization.AllowSort = false;
OrdersGridView.OptionsFind.AllowFindPanel = false;
OrdersGridView.OptionsView.ShowGroupPanel = false;
}
private void MainForm_Load(object sender, EventArgs e) {
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string selectCommandText = @"
select id, name, UnitBrief, UnitPrice, Quantity
from Products
order by id
offset @offset rows
fetch next @size rows only
";
this.DataAdapter = new SqlDataAdapter(selectCommandText, connectionString);
this.DataAdapter.SelectCommand.Parameters.Add("@offset", SqlDbType.Int);
this.DataAdapter.SelectCommand.Parameters.Add("@size", SqlDbType.Int);
this.DataAdapter.SelectCommand.Parameters["@offset"].Value = 0;
this.DataAdapter.SelectCommand.Parameters["@size"].Value = PageSize;
this.DataAdapter.Fill(this.dtOrders);
OrdersGrid.DataSource = dtOrders;
}
private int fPageSize = 10;
public int PageSize {
get { return fPageSize; }
set { fPageSize = value; }
}
private void EmbeddedNavigator_ButtonClick(object sender, NavigatorButtonClickEventArgs e) {
if (e.Button.ButtonType != NavigatorButtonType.Custom) return;
e.Handled = true;
int offset = (int)DataAdapter.SelectCommand.Parameters["@offset"].Value;
string tag = (string)e.Button.Tag;
switch(tag) {
case "NextPage":
offset += PageSize;
break;
case "PrevPage":
offset -= PageSize;
break;
}
offset = Math.Max(0, offset);
OrdersGrid.EmbeddedNavigator.Buttons.CustomButtons[0].Enabled = offset > 0;
DataAdapter.SelectCommand.Parameters["@offset"].Value = offset;
dtOrders.Clear();
DataAdapter.Fill(dtOrders);
OrdersGrid.EmbeddedNavigator.Buttons.CustomButtons[1].Enabled = dtOrders.Rows.Count == PageSize;
}
private void PageSizeEditor_EditValueChanged(object sender, EventArgs e) {
BarEditItem editor = (BarEditItem)sender;
PageSize = int.Parse((string)editor.EditValue);
DataAdapter.SelectCommand.Parameters["@size"].Value = PageSize;
dtOrders.Clear();
DataAdapter.Fill(dtOrders);
}
private void ordersBindingSource_CurrentChanged(object sender, EventArgs e)
{
}
private void OrdersGridView_ColumnFilterChanged(object sender, EventArgs e)
{
GridView gv = sender as GridView;
var rs = gv.ActiveFilterString.Split('=');
}
private void GridView_SubstituteFilter(object sender, SubstituteFilterEventArgs e)
{
e.Filter = GridFilterSubstitutor.Substitute(e.Filter);
}
}
}
Download Code: winforms-xtragrid-sqlpagination
Cảm ơn các bạn đã quan tâm nhé 🤓🤓🤓