【文件属性】:
文件名称:datagridview 分页
文件大小:8KB
文件格式:TXT
更新时间:2012-01-09 19:59:45
datagridview 分页
///
/// 获取总页数
///
/// 总记录数
///
///
public static int GetPageCount(int recordcount, int page_size)
{
int pagecount = 0;
if (recordcount % page_size == 0)
pagecount = recordcount / page_size;
else
pagecount = (recordcount / page_size) + 1;
return pagecount;
}
/**////
/// 获取分页数据
///
/// DATASET
/// 每页记录数
/// 页索引
/// 总页数
///
public static DataView GetPagerForView(DataTable dt, int page_size, int pageindex,out string msg)
{
DataView dv = new DataView();
if (dt != null)
{
int recordCount = dt.Rows.Count; //总记录数
int page_sum = GetPageCount(recordCount, page_size);
if (page_size < dt.Rows.Count)//kl2 :SQL查询函数返回的DATASET
{
if (page_size == 0)//text_intpase :判断用户设置的分页是否合法
page_size = 10;
//recordCount = kl2.Tables[0].Rows.Count;//假设每页只显示1条数据,则共可以显示的页数:pagemark页
if (page_size < 1)
{
msg = "请将分页项设置在[1-" + dt.Rows.Count.ToString() + "]之间";
}
msg = "共" + page_sum.ToString() + "页," + dt.Rows.Count.ToString() + "条";//page_num :lable
DataTable page_table = new DataTable();//记录当前正在操作的是哪个表,全局变量,值由查询函数获取
for (int k = 0; k < dt.Columns.Count; k++)
{
page_table.Columns.Add(dt.Columns[k].ColumnName);
}
if (dt.Rows.Count != 0 && page_size < dt.Rows.Count)
{
page_table.Clear();
try //普通页面显示
{
page_table.Clear();
for (int i = 0; i < page_size; i++)
{
page_table.Rows.Add(dt.Rows[i + (pageindex * page_size)].ItemArray);
}
}
catch //最后不足一个页面的显示
{
page_table.Clear();
try
{
for (int s = 0; s < recordCount - (pageindex * page_size); s++)
{
page_table.Rows.Add(dt.Rows[s + (pageindex * page_size)].ItemArray);
}
}
catch { }
}
msg += " 当前第" + (pageindex + 1).ToString() + "页";
}
dv = page_table.DefaultView;
}
else
{
dv = dt.DefaultView;
msg = "共1页," + dt.Rows.Count.ToString() + "条";
msg += " 当前第" + (pageindex + 1).ToString() + "页";
}
return dv;
}
else
{
msg = "没有数据!";
return null;
}
}
Form中调用:
Code
private DataTable GetDicData()//获取所有据
{
BLLB_CommunitDIC bcdicBll = new BLLB_CommunitDIC();
if (trvDic.SelectedNode != null)
{
if (trvDic.SelectedNode.Parent == null)
{
return null;
}
else
{
int areaid = Convert.ToInt32(trvDic.SelectedNode.Tag);
return FCT.Common.PublicMethods.ConvertToDataSet(bcdicBll.GetAll(areaid)).Tables[0];
}
}
else
return null;
}
//根据页索此获取数据
private void InitData(int pageindex) {
DataTable dt = GetDicData();
string msg;
if (dt != null && dt.Rows.Count > 0)
{
dgrvDic.AutoGenerateColumns = false;
dgrvDic.DataSource = Public.CustomerPager.GetPagerForView(dt, 10, pageindex, out msg);
toolStripLabel18.Text = msg;
}
}
Code
分页事件#region 分页事件
private void toolStrip7_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem.Tag != null)
{
this.Cursor = Cursors.WaitCursor;
int _currentPageIndex = 1;
DataTable dt = GetDicData();
int pagecount = 0;
if (dt != null)
pagecount = Public.CustomerPager.GetPageCount(dt.Rows.Count, 10);
if (toolStripTextBox6.Text != "")
{
try
{
_currentPageIndex = Convert.ToInt32(toolStripTextBox6.Text);
}
catch
{
_currentPageIndex = 1;
}
}
switch (e.ClickedItem.Tag.ToString())
{
case "First":
_currentPageIndex = 1;
break;
case "Prev":
_currentPageIndex = _currentPageIndex - 1;
break;
case "Next":
_currentPageIndex = _currentPageIndex + 1;
break;
case "Last":
_currentPageIndex = pagecount;
break;
default:
this.Cursor = Cursors.Default;
break;
}
InitData(_currentPageIndex - 1);
if (_currentPageIndex == 1) {
toolStripTextBox6.Text = "1";
this.toolStripButton25.Enabled = false;
this.toolStripButton26.Enabled = false;
this.toolStripButton27.Enabled = true;
this.toolStripButton28.Enabled = true;
}
else
{
if (_currentPageIndex < pagecount && _currentPageIndex >= 1)
{
toolStripTextBox6.Text = _currentPageIndex.ToString();
this.toolStripButton25.Enabled = true;
this.toolStripButton26.Enabled = true;
this.toolStripButton27.Enabled = true;
this.toolStripButton28.Enabled = true;
}
else if (_currentPageIndex == pagecount)
{
toolStripTextBox6.Text = pagecount.ToString();
this.toolStripButton25.Enabled = true;
this.toolStripButton26.Enabled = true;
this.toolStripButton27.Enabled = false;
this.toolStripButton28.Enabled = false;
}
else
{
this.toolStripButton25.Enabled = false;
this.toolStripButton26.Enabled = false;
this.toolStripButton27.Enabled = false;
this.toolStripButton28.Enabled = false;
}
}
this.Cursor = Cursors.Default;
}
84 #endregion