winform 自定义分页控件 及DataGridview数据绑定

时间:2023-12-22 22:27:02

winform 自定义分页控件 及DataGridview数据绑定

分页效果如上图所示,用到的控件均为基本控件 ,其方法如下

右击项目-添加-新建项 选择用户控件 然后在用户控件中拖入所需要的Label,Button,Text

winform 自定义分页控件 及DataGridview数据绑定

用户控件全部代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace One.UI
{
public partial class PagerControl : UserControl
{
public PagerControl()
{
InitializeComponent();
}
#region 分页字段和属性 private int pageIndex = ;
/// <summary>
/// 当前页数
/// </summary>
public virtual int PageIndex
{
get { return pageIndex; }
set { pageIndex = value; }
} private int pageSize = ;
/// <summary>
/// 每页记录数
/// </summary>
public virtual int PageSize
{
get { return pageSize; }
set { pageSize = value; }
} private int recordCount = ;
/// <summary>
/// 总记录数
/// </summary>
public virtual int RecordCount
{
get { return recordCount; }
set { recordCount = value; }
} private int pageCount = ;
/// <summary>
/// 总页数
/// </summary>
public int PageCount
{
get
{
if (pageSize != )
{
pageCount = GetPageCount();
}
return pageCount;
}
} #endregion
#region 页码变化时触发事件
public event EventHandler OnPageChanged; #endregion #region 分页及相关事件功能实现 /// <summary>
/// 设窗体控件全部可用
/// </summary>
private void SetFormCtrEnabled()
{
linkFirst.Enabled = true;
linkPrevious.Enabled = true;
linkNext.Enabled = true;
linkLast.Enabled = true;
} /// <summary>
/// 计算总页数
/// </summary>
/// <returns></returns>
private int GetPageCount()
{
if (PageSize == )
{
return ;
}
int pageCount = RecordCount / PageSize;
if (RecordCount % PageSize == )
{
pageCount = RecordCount / PageSize;
}
else
{
pageCount = RecordCount / PageSize + ;
}
return pageCount;
}
/// <summary>
/// 用于客户端调用
/// </summary>
public void DrawControl(int count)
{
recordCount = count;
DrawControl(false);
}
/// <summary>
/// 根据不同的条件,改变页面控件的呈现状态
/// </summary>
private void DrawControl(bool callEvent)
{ txtCurrentPage.Text = PageIndex.ToString();//当前页
lblPageCount.Text = PageCount.ToString();//总页数
lblTotalCount.Text = RecordCount.ToString();//总记录数
txtPageSize.Text = PageSize.ToString();//每页记录数 if (callEvent && OnPageChanged != null)
{
OnPageChanged(this, null);//当前分页数字改变时,触发委托事件
}
SetFormCtrEnabled();
if (PageCount == )//有且仅有一页时
{
linkFirst.Enabled = false;
linkPrevious.Enabled = false;
linkNext.Enabled = false;
linkLast.Enabled = false;
}
else if (PageIndex == )//当前页为第一页时
{
linkFirst.Enabled = false;
linkPrevious.Enabled = false;
}
else if (PageIndex == PageCount)//当前页为最后一页时
{
linkNext.Enabled = false;
linkLast.Enabled = false;
}
} #endregion #region 相关控件事件 //首页按钮
private void linkFirst_Click(object sender, EventArgs e)
{
PageIndex = ;
DrawControl(true);
} //上一页按钮
private void linkPrevious_Click(object sender, EventArgs e)
{
PageIndex = Math.Max(, PageIndex - );
DrawControl(true);
} //下一页按钮
private void linkNext_Click(object sender, EventArgs e)
{
PageIndex = Math.Min(PageCount, PageIndex + );
DrawControl(true);
} //尾页按钮
private void linkLast_Click(object sender, EventArgs e)
{
PageIndex = PageCount;
DrawControl(true);
} /// <summary>
/// 按下enter键,执行跳转页面功能
/// </summary>
private void txtPageNum_KeyPress(object sender, KeyPressEventArgs e)
{
int num = ;
if (int.TryParse(txtCurrentPage.Text.Trim(), out num) && num > )
{
PageIndex = num;
DrawControl(true);
}
} /// <summary>
/// 跳转页数限制
/// </summary>
private void txtCurrentPage_TextChanged(object sender, EventArgs e)
{
int num = ;
if (int.TryParse(txtCurrentPage.Text.Trim(), out num) && num > )
{ //TryParse 函数,将字符串转换成等效的整数,返回bool型,判断是否转换成功。
//输入除数字以外的字符是转换不成功的 if (num > PageCount) //输入数量大于最大页数时,文本框自动显示最大页数
{
txtCurrentPage.Text = PageCount.ToString();
}
}
} #endregion
bool isTextChanged = false;
/// <summary>
/// 每页显示的记录数改变时
/// </summary>
private void txtPageSize_TextChanged(object sender, EventArgs e)
{
int num = ;
//输入不符合规范时,默认设置为100
if (!int.TryParse(txtPageSize.Text.Trim(), out num) || num <= )
{
num = ;
txtPageSize.Text = "";
}
else
{
isTextChanged = true; }
pageSize = num;
}
/// <summary>
/// 光标离开 每页设置文本框时,显示到首页
private void txtPageSize_Leave(object sender, EventArgs e)
{
if (isTextChanged)
{
isTextChanged = false;
linkFirst_Click(null, null);
}
} }
}

用户控件完成后,生成解决方案就会在左侧工具栏中看到刚写好的分页控件,拖入到需要分页的窗体中赋值调用即可;

在窗体加载事件中激活分页控件方法

 private void OneForm_Load(object sender, EventArgs e)
{
ShowData();
//激活OnPageChanged事件
pagerControl1.OnPageChanged += new EventHandler(pagerControl1_OnPageChanged); }

分页控件页码发生变化时执行方法

 private void pagerControl1_OnPageChanged(object sender, EventArgs e)
{
ShowData();
}

绑定Datagridview方法

 private void ShowData()
{
int count;///4.6日
string strWhere = " 1=1 ";
//可以添加where条件 pagerControl1.PageSize = ;//设置显示记录数
ds = bll.GetList(pagerControl1.PageIndex, pagerControl1.PageSize, strWhere, out count);//4.6
dataGridViewX1.DataSource = ds.Tables[];
pagerControl1.DrawControl(count);
}

bll.GetList为数据读取返回dataset类型

 public DataSet GetList(int PageIndex, int PageSize, string strWhere, out int count)
{
StringBuilder strSql = new StringBuilder();
//strSql.Append(" select * from ( ");
strSql.Append(" select ROW_NUMBER() over(order by Material_TimeAdd) num,* ");
strSql.Append(" FROM tabel");
if (strWhere.Trim() != "")
{
strSql.Append(" where " + strWhere);
}
count = DbHelperSQL.Query(strSql.ToString()).Tables[].Rows.Count; strSql.Append(" ) t where t.num between " + ((PageIndex - ) * PageSize + ) + " and " + PageSize * PageIndex);
DataSet ds = DbHelperSQL.Query(" select * from ( " + strSql.ToString()); return ds; }

第一次写东西,希望能够帮助需要的人。。。。。。