如何为datagridview增加序号列
1个回答
展开全部
你可以重写DataGridView的OnRowPostPaint方法或者直接在DataGridView的RowPostPaint事件里写,如下(重写DataGridView的OnRowPostPaint方法)
using System;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace Test
{
class DataGridViewEx : DataGridView
{
SolidBrush solidBrush;
public DataGridViewEx()
{
solidBrush = new SolidBrush(this.RowHeadersDefaultCellStyle.ForeColor);
}
protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
{
e.Graphics.DrawString((e.RowIndex + 1).ToString(), e.InheritedRowStyle.Font, solidBrush, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + 5);
base.OnRowPostPaint(e);
}
}
}
using System;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace Test
{
class DataGridViewEx : DataGridView
{
SolidBrush solidBrush;
public DataGridViewEx()
{
solidBrush = new SolidBrush(this.RowHeadersDefaultCellStyle.ForeColor);
}
protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
{
e.Graphics.DrawString((e.RowIndex + 1).ToString(), e.InheritedRowStyle.Font, solidBrush, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + 5);
base.OnRowPostPaint(e);
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询