- 积分
- 40165
- 好友
- 记录
- 主题
- 帖子
- 听众
- 收听
|
发表于 2013-5-11 22:25:08
|
显示全部楼层
把你的[code=csharp] private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
using (SolidBrush b = new SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor))
{
int linen = 0; linen = e.RowIndex + 1;
string line = linen.ToString();
e.Graphics.DrawString(line, e.InheritedRowStyle.Font, b, e.RowBounds.Location.X, e.RowBounds.Location.Y + 5);
SolidBrush B = new SolidBrush(Color.Red);
}
}[/code]
代码换成如下代码试试
[code=csharp] private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dataGridView1.RowHeadersWidth - 4, e.RowBounds.Height);
TextRenderer.DrawText(
e.Graphics, (e.RowIndex + 1).ToString(),
dataGridView1.RowHeadersDefaultCellStyle.Font,
rectangle,
dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter
);
}
[/code]
如果你是使用Table绑定的Gridview建议使得如下方法
[code=csharp] //实现功能 DataGridView 添加 自动编号
DataTable table =new DataTable();
DataColumn column = new DataColumn();
column.AutoIncrement = true; //AutoIncrement 获取或设置一个值,该值指示对于添加到该表中的新行,列是否将列的值自动递增
column.ColumnName = "自动编号";
column.AutoIncrementSeed = 1;
column.AutoIncrementStep = 1;
table.Columns.Add(column);
table.Merge(table);//Merge合并DataTable
this.dataGridView1.DataSource = table; [/code]
|
|