有很多种方法,这里介绍三种:
A:
控件的RowStateChanged事件中添加,RowStateChanged事件是在行的状态更改(例如,失去或获得输入焦点)时发生的事件:
1 e.Row.HeaderCell.Value = (e.Row.Index + 1).ToString();//添加行号
2
3 //e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1);
B:
使用控件的RowPostPaint事件,RowPostPaint事件是在绘制 System.Windows.Forms.DataGridViewRow (所有单元格绘制,执行了行绘制之后)发生的事件,不过使用RowPostPaint事件这种方法在数据量很大的时候性能比较差,每次滚动数据都会触发RowPostPaint事件,绘制行号。
(主要利用TextRenderer.DrawText()方法,使用指定的设备上下文、字体、颜色和格式说明在指定界限中绘制指定文本。)
1 private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
2 {
3 //添加行号
4 System.Drawing.Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Y, this.dataGridView1.RowHeadersWidth - 4, e.RowBounds.Height);
5 TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), this.dataGridView1.RowHeadersDefaultCellStyle.Font, rectangle, this.dataGridView1.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);//”TextFormatFlags.VerticalCenter | TextFormatFlags.Right“中“|”有增加的作用,此处添加了两种文本字符格式样式
6 //this.dataGridView1.RowHeadersDefaultCellStyle.BackColor = Color.FromArgb(192, 192, 255);//行标题单元格BackColor
7 //this.dataGridView1.RowHeadersDefaultCellStyle.BackColor = SystemColors.Control;
8 }
B)2):
1 private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)View Code
2 {
3 var rowIdx = (e.RowIndex + 1).ToString();
4 var centerFormat = new StringFormat()
5 {
6 Alignment = StringAlignment.Center,
7 LineAlignment = StringAlignment.Center,
8 };
9 var headerBounds = new Rectangle(e.RowBounds.Left, e.RowBounds.Top, dataGridView1.RowHeadersWidth, e.RowBounds.Height);
10 e.Graphics.DrawString(rowIdx, this.Font, SystemBrushes.ControlText, headerBounds, centerFormat);
11 }
C:
在控件的RowsAdded事件和RowsRemoved事件中添加代码。
RowsAdded事件是 在向 System.Windows.Forms.DataGridView 中添加新行之后发生。
1 private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
2 {
3
4 for (int i = 0; i < e.RowCount; i++)
5 {
6 dataGridView1.Rows[e.RowIndex + i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
7 dataGridView1.Rows[e.RowIndex + i].HeaderCell.Value = (e.RowIndex + i + 1).ToString();
8 }
9
10 for (int i = e.RowIndex + e.RowCount; i < this.dataGridView1.Rows.Count; i++)
11 {
12 dataGridView1.Rows[i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
13 dataGridView1.Rows[i].HeaderCell.Value = (i + 1).ToString();
14 }
15 }
RowsRemoved事件是 在从 System.Windows.Forms.DataGridView 中删除一行或多行时发生。
1 private void dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
2 {
3 for (int i = 0; i < e.RowCount; i++)
4 {
5 dataGridView1.Rows[e.RowIndex + i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
6 dataGridView1.Rows[e.RowIndex + i].HeaderCell.Value = (e.RowIndex + i + 1).ToString();
7 }
8
9 for (int i = e.RowIndex + e.RowCount; i < this.dataGridView1.Rows.Count; i++)
10 {
11 dataGridView1.Rows[i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
12 dataGridView1.Rows[i].HeaderCell.Value = (i + 1).ToString();
13 }
14 }
注:DataGridView控件的RowsHeaderWidthSizeMode属性可以设置如何调整行标题宽度,设置为AutoSizeToAllHeaders或者AutoSizeToDisplayedHeaders,会自动设置宽度不会出现行指示符挤压行号的情况。