国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

現windows應用程序中用的datagrid具有分頁功能

2024-07-21 02:22:41
字體:
來源:轉載
供稿:網友

工作需要,要實現windows應用程序中用的datagrid具有分頁功能,不知道ms怎么想的,asp.net的datagrid有這樣的功能,為什么不在winform的datagrid里面提供這樣的功能,還得讓我費這么大勁兒來重寫這個控件,真是麻煩。

首先,我們要做一個類來繼承系統的datagrid:

using system;

using system.drawing;

using system.collections;

using system.componentmodel;

using system.windows.forms;

using system.data;

 

namespace oilx.pagingdatagrid

{

public class pagingdatagrid : datagrid

{

  // 聲明一些必要的全局變量。

private system.componentmodel.container components = null;

 

                      /// <summary>

                      ///                  

/// </summary>

                      private const int markerwith = 14;

 

                      /// <summary>

                      ///

                      /// </summary>

                      private point      _pointtopleft; 

 

                      /// <summary>

                      ///

                      /// </summary>

                      private int _row = 1;

 

                      //

                      private int _rownumberfigure = 0;

                      private bool _zeropadding = false;

 

                      /// <summary>

                      ///

                      /// </summary>

                      private string _rowheadercaption = string.empty;

 

                      /// <summary>

                      ///

                      /// </summary>

                      private int _pagesize = 10;

 

                      /// <summary>

                      ///

                      /// </summary>

                      private int _currentpagecount = 1;

 

                      /// <summary>

                      ///

                      /// </summary>

                      private bool _allowpaging = false;

 

                      /// <summary>

                      ///

                      /// </summary>

                      private int _previouspagecount = -1;

                      /// <summary>

                      ///

                      /// </summary>

                      private datatable _pagedatasource;

 

/// <summary>

///構造函數

/// </summary>

/// <param name="container"></param>

public pagingdatagrid (system.componentmodel.icontainer container)

{

           container.add(this);

           initializecomponent();

           initiarizecontrol();

}

/// <summary>

///構造函數重載

/// </summary>

public fixditemdatagrid()

{

           initializecomponent();

           initiarizecontrol();

                                 

}

// 下面我們需要增加一些用于分頁的公共屬性

/// <summary>

/// 頁面大小,即每頁現實的數據條數

/// </summary>

[browsable(true),description("頁面大小")]

public int pagesize

{

           get

           {

                      return _pagesize;

           }

           set

           {

                      _pagesize = value;

           }

}

/// <summary>

/// 分頁數

/// </summary>

[browsable(true),description("分頁數")]

public int pagecount

{

           get

           {

                      if(datasource == null)

                      {

                                  return 0;

                      }

                      if(_pagedatasource == null || _pagedatasource.rows.count <= 0)

                      {

                                  return 0;

                      }

                      int i = _pagedatasource.rows.count%pagesize==0?0:1;

                      return _pagedatasource.rows.count/pagesize+i;

           }

}

/// <summary>

/// 當前頁數

/// </summary>

[browsable(true),description("當前頁數")]

public int currentpagecount

{

           get

           {

                      if(_currentpagecount < 0)

                      {

                                  return 1;

                      }

                      if(_currentpagecount >= pagecount)

                      {

                                  return pagecount;

                      }

                      return _currentpagecount;

           }

           set

           {

                      if(value >=pagecount)

                      {

                                  _currentpagecount =  pagecount;

                      }

                      _currentpagecount = value;

           }

}

/// <summary>

///是否允許分頁

/// </summary>

[browsable(true),description("是否允許分頁")]

public bool allowpaging

{

           get

           {

                      return _allowpaging;

           }

           set

           {

                      _allowpaging = value;

           }

}

/// <summary>

///行標題

/// </summary>

[browsable(true),description("行標題")]

public string rowheadercaption

{

           get

           {

                      return _rowheadercaption;

           }

           set

           {

                      _rowheadercaption = value;

           }

}

 

// 基本工作都作完了,下面開始我們分頁的重點,go!

// 我們需要重寫系統一個方法,那就是:

/// <summary>

/// 該方法在數據源被修改的時候激活。

/// </summary>

/// <param name="e"></param>

protected override void ondatasourcechanged(system.eventargs e)

{

           base.ondatasourcechanged(e);

           // 如果數據源為空那么就返回。

           if( this.datasource == null )

           {

                      return;

           }

           // 下面判斷數據源的類型,一般情況下,datagrid數據源形式為3種,1,dataset,2,datatable,3,dataview。

           // 在這里我們要做的是把數據源的數據保存到另外一個變量里面,這樣當我們在作分頁

           //的時候,需要修改數據源時,可以把這個變量當作原始的數據源來進行判斷。

           if(this.datasource is dataset)

           {

                      if( ((dataset)datasource).tables.count > 0)

                      {

                                  this.datasource = ((dataset)datasource).tables[0];

                      }

                      else

                      {

                                  return;

                      }

           }

           if(_pagedatasource == null)

           {

                      if(datasource is dataset)

                      {

                                                        if(((dataset)datasource).tables[0].rows.count <=0)

           {

                      return;

           }

           _pagedatasource = ((dataset)datasource).tables[0].clone();

           for(int i = 0;i<((dataset)datasource).tables[0].rows.count;i++)

           {

                      datarow drrow = _pagedatasource.newrow();

                      drrow.itemarray = ((dataset)datasource).tables[0].rows[i].itemarray;

                      _pagedatasource.rows.add(drrow);

           }

}

           else if(datasource is datatable)

           {

                      if(((datatable)datasource).rows.count <= 0)

                      {

                                  return;

                      }

                      _pagedatasource = ((datatable)datasource).clone();

           for(int i = 0;i<((datatable)datasource).rows.count;i++)

           {

                      datarow drrow = _pagedatasource.newrow();

                      drrow.itemarray = ((datatable)datasource).rows[i].itemarray;

                      _pagedatasource.rows.add(drrow);

           }

}

else if(datasource is dataview)

{                    

if( ((dataview)datasource).table.rows.count <= 0)

           {

                      return;

           }

           _pagedatasource = ((dataview)datasource).table.clone();

           for(int i = 0;i<((dataview)datasource).table.rows.count;i++)

           {

                      datarow drrow = _pagedatasource.newrow();

                      drrow.itemarray = ((dataview)datasource).table.rows[i].itemarray;

                      _pagedatasource.rows.add(drrow);

           }

}

}

_pointtopleft = new point(this.getcellbounds(0,0).x + 4, this.getcellbounds(0,0).y + 4);

}

// 上面的所作的只是把原數據源數據導入到_pagedatasource這個datatable里面,在這里有

//人可能要問為什么不用 _pagedatasource = (datatable)this.datasource;

// 而使用

//datarow drrow = _pagedatasource.newrow();

//drrow.itemarray = ((dataview)datasource).table.rows[i].itemarray;

//_pagedatasource.rows.add(drrow);

//這是因為datatable是引用類型,如果直接付值的話,那么當原數據源的數據被修改時,——//_pagedatasource也會被修改。

//好了,臨時數據已經處理完了,那么真正的分頁處理開始,大家都知道,.net控件要顯示在窗體//上,那么需要我們把它的實體畫上去,那么ms.net在所有控件都提供了下面這樣的方法,可以//讓我們把控件按照我們修改后的形式表現出來:

/// <summary>

///

/// </summary>

/// <param name="e"></param>

protected override void onpaint(system.windows.forms.painteventargs e)

{

base.onpaint(e);

           if( this.datasource == null )

           {

                      return;

           }

           if(datasource != null && datasource is dataset)

           {

                      return;

           }

           // 在這里我們開始對分頁進行進行處理          

           if(allowpaging)

           {

                      if(_pagedatasource != null)

                      {

                                  if(_previouspagecount != currentpagecount)

                                  {

                                             // 判斷當前是第幾頁

                                             int i = currentpagecount*pagesize-pagesize;

                                             if(i<0)

                                             {

                                                        i = 0;

                                             }

//下面判斷是否為最后一頁

                                             if(i <= _pagedatasource.rows.count)

                                             {

                                                        datatable dtpage = _pagedatasource.clone();

                                                        int icount = 0;

           根據pagesize來生成一個datatable,重新進行數據源綁定。

           for(;i<_pagedatasource.rows.count;i++)

           {

                                                                             

                      datarow drrow = dtpage.newrow();

                                                                                          drrow.itemarray= _pagedatasource.rows[i].itemarray;

                                                                                          dtpage.rows.add(drrow);

                      icount ++;

                                                                                          if(icount>=pagesize)

           {

                      break;

           }

           }

           // 綁定新的數據源

           this.datasource = dtpage;

}

_previouspagecount = currentpagecount;

}

}

}

           // rowheaders如果為true,那么我們要給rowheader上面標上序號和標題

           if(this.rowheadersvisible)

           {

                      datagrid.hittestinfo hti = this.hittest(_pointtopleft);

                      int irowhti = hti.row;

                      if(irowhti < 0)

                      {

                                  irowhti = 0;

                      }

                      int idelta = this.getcellbounds(irowhti, 0).height + 1;

                      int iheight = this.getcellbounds(irowhti, 0).top + 1;

                      // 這個方法里面是顯示rowheaders的標題,詳見該方法實現

                      showcolumntitles(e);

                      currencymanager cm = (currencymanager) this.bindingcontext[this.datasource, this.datamember];

                      while(iheight < this.height - idelta && irowhti < cm.count)

                      {

                                  string strtext = string.empty;

                                  if(_zeropadding)

                                  {

                                             strtext = string.format("{0}", _row + irowhti).padleft(_rownumberfigure, '0');

                                  }

                                  else

                                  {

                                             strtext = string.format("{0}", _row + irowhti);

                                  }

                                  e.graphics.drawstring(strtext, this.font, new solidbrush(color.black), 20, iheight);

                                  // 下面處理的是在rowheaders上面畫一條分割線,為了有陰影的感覺,我畫了一條灰色和白色兩條,看著還挺像 ^_^

                                  if(!this.captionvisible)

                                  {

                                            showsepalatorline(e, new point(17, 2), new point(17, iheight-2 + this.preferredrowheight),color.gray);

                                             showsepalatorline(e, new point(18, 2), new point(18, iheight-2 + this.preferredrowheight),color.white);

                                  }

                                  else

                                  {

                                             showsepalatorline(e, new point(17, 20), new point(17, iheight-2 + this.preferredrowheight),color.gray);

                                            showsepalatorline(e, new point(18, 20), new point(18, iheight-2 + this.preferredrowheight),color.white);

                                  }

                                  iheight += idelta;

                                  irowhti++;

                      }

                      _row =currentpagecount*pagesize-pagesize+1;

                      if(_row < 0)

                      {

                                  _row =1;

                      }

           }

}

// 畫線

private void showsepalatorline(painteventargs e, point ptstart, point ptend,color linecolor)

{

           pen pen = new pen(new solidbrush(linecolor), 1);

           e.graphics.drawline(pen, ptstart, ptend);

}

/// <summary>

/// 顯示rowheaders的標題

/// </summary>

/// <param name="e"></param>

/// <param name="source"></param>

private void showcolumntitles(painteventargs e)

{

           string strheader = rowheadercaption;

           int ix     = markerwith + 5;

           int iwith  = this.rowheaderwidth;

           using(stringformat sf = new stringformat())

           {

                      sf.formatflags   = stringformatflags.nowrap;

                      sf.trimming      = stringtrimming.ellipsischaracter;

                      sf.alignment     = stringalignment.near;

                      sf.linealignment = stringalignment.center;

                      rectanglef rect;

                      if(!captionvisible)

                      {

                                  rect = new rectanglef(new pointf(ix, 3), new sizef(iwith, this.preferredrowheight));

                      }

                      else

                      {

                                  rect = new rectanglef(new pointf(ix, 20), new sizef(iwith, this.preferredrowheight));

                      }

                      e.graphics.drawstring(strheader, this.font, new solidbrush(color.black), rect, sf);

           }

}

/// <summary>

/// ?}?e?x?|?c?“?^‚???“®¸?—¯

/// </summary>

/// <param name="e"></param>

protected override void onmousemove(system.windows.forms.mouseeventargs e)

{

           datagrid.hittestinfo hittestinfo = this.hittest(new point(e.x, e.y))          if(hittestinfo.type == datagrid.hittesttype.rowresize)

           {

                      return;

           }

           base.onmousemove(e);

                      }

 

/// <summary> ///

/// </summary>

/// <param name="e"></param>

protected override void onmousedown(system.windows.forms.mouseeventargs e)

{

           datagrid.hittestinfo hittestinfo = this.hittest(new point(e.x, e.y));

           if(hittestinfo.type == datagrid.hittesttype.rowresize)

           {

                      return;

           }

           base.onmousedown(e);

}         

}

} 好了,到這里這個,這個分頁的datagrid就算完了。程序實現:datagrid1.currentpagecount ++;datagrid1.invalidate();一定要寫datagrid1.invalidate();這個方法,不然datagrid不能刷新,看不到分頁效果!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 正定县| 塘沽区| 三明市| 岗巴县| 邳州市| 贞丰县| 延安市| 砀山县| 富顺县| 吉木萨尔县| 巨野县| 宁陕县| 西乌| 神木县| 临潭县| 江津市| 阳朔县| 东乡族自治县| 武定县| 定边县| 新巴尔虎左旗| 双牌县| 山阴县| 渭南市| 肇州县| 巫溪县| 阆中市| 什邡市| 宁陵县| 昌都县| 阿合奇县| 长岭县| 抚顺县| 宝应县| 临江市| 丁青县| 大城县| 内黄县| 罗江县| 衡南县| 宜宾县|