DataGrid(WinForm)显示行号最简单的方法

80酷酷网    80kuku.com

  datagrid|显示同样是重载OnPaint 方法,但是方法应该是比较巧妙的!而且不用担心标题是不是有显示,也不用去计算坐标,很方便的说!


protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);

if(this.DataSource!=null)
{
if( this.VisibleRowCount == 0 )return;

Rectangle currRct;

int iRowCount = this.VisibleRowCount;

string sText = "";

int nowY = 0;

for( int i = 0 ; i < iRowCount ; i++ )
{
currRct = (Rectangle)this.GetCellBounds( i, 0 );
nowY = currRct.Y + 2;
sText = string.Format( " {0}", i+1 );
e.Graphics.DrawString( sText, this.Font, new SolidBrush(Color.Black), 10, nowY );
}

}
}



分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: