日期:2014-05-18  浏览次数:20465 次

gridview自带分页 如何控制位置?我想把它搞在外面
gridview自带分页 如何控制位置?我想把它搞在外面

------解决方案--------------------
如果你想任意的调整位置的话还是别用gridview了,分页又丑又不好,listview的自带DataPager分页不错,任意调整都可以,所以建议你换了吧
------解决方案--------------------
探讨

如果你想任意的调整位置的话还是别用gridview了,分页又丑又不好,listview的自带DataPager分页不错,任意调整都可以,所以建议你换了吧

------解决方案--------------------
自己写一个吧
------解决方案--------------------
WinForm的这个控件不具备分页功能,而WebForm的用这个功能,使用者却嫌难看,真是难为微软了。

下面是我WinForm自己实现的:
1、分页的存储过程:

create proc PageNum
@PageSize int, --每页显示条数
@PageIndex int, --当前是第几页
@totalRows int output, --总行数
@totalPages int output --总页数
as
declare @startId int
declare @endId int
set @startId = @PageSize*(@PageIndex-1)+1
set @endId = @startId+@PageSize-1
select @totalRows=COUNT(*) from Books
set @totalPages=@totalRows/@PageSize
if(@totalRows%@PageSize != 0) --如果总行数除每页显示数据量的值不等于0,则总页数得加1
begin
set @totalPages=@totalPages+1
end
declare @IndexTable table(Id int identity(1,1) ,nId int)
insert into @IndexTable select Id from Books
select * from @IndexTable as t ,Books as b where t.Id>=@startId and t.Id<=@endId and t.nId=b.Id
go
 
declare @tr int --总行数
declare @tp int --总页数
exec PageNum 4,2,@tr output ,@tp output
print '这是总页数'+convert(varchar,@tp) --数据转换
print @tr

2、不一定要用存储过程,可以一次性读到DataTable后,再做处理
C# code

 private int pageSize = 0; //每页显示行数
  private int pageCurrent = 0; //当前页号
  private int pageCount = 0; //页数=总记录数/每页显示行数
  private int nCurrent = 0; //当前记录行号
  private int nMax = 0; //总记录数
  private DataTable srcTable = null; //元数据

  private void First_Click(object sender, EventArgs e)
  {
  try
  {
  pageCurrent = 1; //当前页数从1开始
  nCurrent = 0; //当前记录数从0开始

  if (srcTable == null || srcTable.Rows.Count == 0)
  return;

  DataTable table = LoadPageData();
  ChangeDataSoure(table);

  First.Enabled =false;
  Prev.Enabled = false;
  Next.Enabled = true;
  End.Enabled = true;
  }
  catch (Exception ex)
  {
  MessageBox.Show(ex.Message);
  }
  }

  private void Prev_Click(object sender, EventArgs e)
  {
  try
  {
  pageCurrent--;
  nCurrent = pageSize * (pageCurrent - 1);

  if (srcTable == null || srcTable.Rows.Count == 0)
  return;
  DataTable table = LoadPageData();
  ChangeDataSoure(table);

  bool b = pageCurrent <= 1 ? false : true;
  First.Enabled = b;
  Prev.Enabled = b;
  Next.Enabled = true;
  End.Enabled = true;
  }
  catch (Exception ex)
  {
  MessageBox.Show(ex.Message);
  }
  }

  private void Next_Click(object sender, EventArgs e)
  {
  try
  {
  pageCurrent++;
  nCurrent = pageSize * (pageCurrent - 1);

  if (srcTable == null || srcTable.Rows.Count == 0)
  return;
  DataTable table = LoadPageData();
  ChangeDataSoure(table);

  bool b = pageCurrent >= pageCount ? false : true;
  Next.Enabled = b;
  End.Enabled = b;

  First.Enabled = true;
  Prev.Enabled = true;
  }
  catch (Exception ex)
  {
  MessageBox.Show(ex.Message);
  }
  }

  private void End_Click(object sender, EventArgs e)
  {
  try
  {
  pageCurrent = pageCount;
  nCurrent = pageSize * (pageCurrent - 1);

  if (srcTable == null || srcTable.Rows.Count == 0)
  return;

  DataTable table = LoadPageData();
  ChangeDataSoure(table);

  Next.Enabled =false;
  End.Enabled = false;
  First.Enabled = true;
  Prev.Enabled = true;
  }
  catch (Exception ex)
  {
  MessageBox.Show(ex.Message);
  }
  }

  private void InitPageSet()
  {
  pageSize = 30; //设置页面行数
  nMax = srcTable.Rows.Count;
  pageCount = (nMax / pageSize); //计算出总页数

  if ((nMax % pageSize) > 0)
  {
  pageCount++;
  }

  pageCurrent = 1; //当前页数从1开始
  nCurrent = 0; //当前记录数从0开始

  First.Enabled =false;
  Prev.Enabled = false;

  bool b = pageCurrent == pageCount ? false : true;
  Next