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

类别:.NET开发 点击:0 评论:0 推荐:

同样是重载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://com.8s8s.com/it/it41903.htm