DataGridView大扩展——显示行号

原文 DataGridView大扩展——显示行号

在DataGridView 的实际使用中,经常需要标示出行号,这样可以比较醒目地看到当前信息。不过DataGridView 在绘制 DataGridViewRow 时没有处理行号,要实现这种效果,需要使用RowPostPaint事件。

  主要代码如下:

  

复制代码
    private bool _isShowLineNumber;

        void DataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)

        {

            if (_isShowLineNumber)

            {

                string title = (e.RowIndex + 1).ToString();

                Brush bru = Brushes.Black;

                e.Graphics.DrawString(title, DefaultCellStyle.Font,

                    bru, e.RowBounds.Location.X + RowHeadersWidth / 2 - 4, e.RowBounds.Location.Y + 4);

            }

        }
复制代码

你可能感兴趣的:(datagridview)