标签:
为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);
}
}
}
}
标签:
原文地址:http://blog.csdn.net/fyshk/article/details/45363981