111,126
社区成员
发帖
与我相关
我的任务
分享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;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);
}
}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;
}
}
private void grid_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
e.RowBounds.Location.Y,
grid.RowHeadersWidth - 4,
e.RowBounds.Height);
TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
grid.RowHeadersDefaultCellStyle.Font,
rectangle,
grid.RowHeadersDefaultCellStyle.ForeColor,
TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
}