该功能主要用到了DataGridView的 RowPostPaint事件,为动态描绘,经测试,效率基本满意。该事件直接调用静态方法DrawingRowIndex(DataGridView dgv, DataGridViewRowPostPaintEventArgs e),传入datagridview实例和参数e.
public static void DrawingRowIndex(DataGridView dgv, DataGridViewRowPostPaintEventArgs e)
{
//描绘文本的位置
Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
e.RowBounds.Location.Y,
dgv.RowHeadersWidth - 13,//行头宽
e.RowBounds.Height);//宽
//描绘行头文本
TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),//内容
dgv.RowHeadersDefaultCellStyle.Font,//字体
rectangle,//位置
dgv.RowHeadersDefaultCellStyle.ForeColor,//颜色
TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
}
转帖自
http://blog.youkuaiyun.com/A1027/archive/2008/08/05/2768885.aspx