方法一:处理数据源,假设原来绑定的数据源为DataTable dt
方法二:
方法三:
1
2
3
4
5
6
7
8
9
|
DataTable ndt =
new
DataTable();
DataColumn dc =
new
DataColumn();
dc.ColumnName =
"序号"
;
dc.AutoIncrement =
true
;
dc.AutoIncrementSeed = 1;
dc.AutoIncrementStep = 1;
ndt.Columns.Add(dc);
ndt.Merge(dt);
dataGridView1.DataSource = ndt;
|
方法二:
1
2
3
4
5
6
7
8
9
10
11
12
|
private
void
dataGridView1_RowPostPaint(
object
sender, DataGridViewRowPostPaintEventArgs e)
{
using
(SolidBrush b =
new
SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor))
{
string
linenum = e.RowIndex.ToString();
int
linen = 0;
linen = Convert.ToInt32(linenum) + 1;
string
line = linen.ToString();
e.Graphics.DrawString(line, e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 5);
SolidBrush B =
new
SolidBrush(Color.Red);
}
}
|
方法三:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
private
void
dataGridView1_CellPainting(
object
sender, DataGridViewCellPaintingEventArgs e)
{
if
(e.ColumnIndex < 0 && e.RowIndex >= 0)
{
e.Paint(e.ClipBounds, DataGridViewPaintParts.All);
Rectangle indexRect = e.CellBounds;
indexRect.Inflate(-2, -2);
TextRenderer.DrawText(e.Graphics,
(e.RowIndex + 1).ToString(),
e.CellStyle.Font,
indexRect,
e.CellStyle.ForeColor,
TextFormatFlags.Right | TextFormatFlags.VerticalCenter);
e.Handled =
true
;
}
}
|