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

发表于:2007-06-30来源:作者:点击数: 标签:
同样是重载OnPaint 方法,但是方法应该是比较巧妙的!而且不用担心标题是不是有显示,也不用去计算坐标,很方便的说! protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if(this.DataSource!=null) { if( this.VisibleRowCount == 0 )return
同样是重载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 );
}

}
}



原文转自:http://www.ltesting.net