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

同样是重载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 ); 
        }
    }
}
Contributors: FHL