码迷,mamicode.com
首页 > Windows程序 > 详细

winfrom 为datagridview 添加行号

时间:2015-04-29 13:43:25      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:

为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);
            }

        }

    }
}



winfrom 为datagridview 添加行号

标签:

原文地址:http://blog.csdn.net/fyshk/article/details/45363981

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!