程序员人生 网站导航

winfrom 为datagridview 添加行号

栏目:综合技术时间:2015-06-09 08:49:41

为datagridview添加行号

1. 注册datagridview的RowPostPaint事件

2. 在事件内行动画上行号


using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CommonUtil
{
    public class DataGridViewUtil
    {
        /// <summary>
        /// 为datagridView行添加行号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void DataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            var dataGridView1 = (DataGridView)sender;
            Color color = dataGridView1.DefaultCellStyle.ForeColor;
            if (dataGridView1.Rows[e.RowIndex].Selected)
                color = dataGridView1.DefaultCellStyle.SelectionForeColor;
            else
                color = dataGridView1.DefaultCellStyle.ForeColor;

            using (SolidBrush b = new SolidBrush(color))
            {
                e.Graphics.DrawString((e.RowIndex + 1).ToString(), e.InheritedRowStyle.Font, b,
                    e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 6);
            }

        }

    }
}


------分隔线----------------------------
------分隔线----------------------------

最新技术推荐