首页 > Windows开发 > 详细

winfrom 为datagridview 添加行号

时间:2015-04-29 13:41:11      阅读:341      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!