显示DataGrid序号的一个适用的方法
如果數據量小的話沒有問題,一旦數據量大,顯示特別慢,還有個缺點就是拖動行高時行號不隨行高的變化而變動,出現是幾個序號在一個單元格中顯示。我自己對他們的算法進行總結,寫出一個效果比較不錯的帶序號的 DataGrid 。原理:只顯示表格中顯示行的序號,并且拖動行,行號一起移動。
override protected void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
??? try
??? {
??? ??? if(this.DataSource!=null)
??????? {??????????
??????? int yDelta;
??????? System.Drawing .Rectangle cell=this.GetCellBounds(0,0);
??????? int y=cell.Top +2;
??????? e.Graphics.DrawString("編號", this.Font, new SolidBrush(Color.Black), 8, y-18); //
??????? if(this.VisibleRowCount >0)//只在有記錄集時在表格中顯示序號
??????? {?????????? ???????
??????????? CurrencyManager cm;
??????????? cm = (CurrencyManager) this.BindingContext[this.DataSource, this.DataMember];
??????????? if(cm.Count >0)
??????????? {??????????????????????
??????????????? int nRow=-1;
??????????????? y=41;?????????? //為第一行默認高度
??????????????? while(nRow<0)
??????????????? {
nRow=this.HitTest (8,y).Row ;
??????????????????? y++;
??????????????? }
??????????????? int nCount=0;
??????????????? while(y<this.Height && nCount<this.VisibleRowCount )
??????????????? {
??????????????????? string text = string.Format("{0}", nRow+nCount+1);
??????????????????? e.Graphics.DrawString(text, this.Font, new SolidBrush(Color.Black), 10, y);
??????????????????? yDelta = this.GetCellBounds( nRow+nCount,0).Height + 1;//****表示一行高度的參數
??????????????????? y += yDelta;
//如果下面有子行顯示序號的區分顯示??
??????????????????? if(this.IsExpanded (nRow+nCount)&& nRow+nCount+1<cm.Count )???????????? ??? ??????? ??????? {
??????????????????????? y+=this.GetCellBounds (nRow+nCount+1,0).Height +3;
??????????????????? }
??????????????????? nCount++; ?????
??????????????? }
}
??????? }
??????? }
??? }
??? catch
??? {}
}
重載了DataGrid中的Paint,這樣用起來會特別方便,區區雕蟲小技,希望和大家共同分享。
總結
以上是生活随笔為你收集整理的显示DataGrid序号的一个适用的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DataGrid连接Access的快速分
- 下一篇: asp.net中DataGrid性能测试