DataGridView控件的屬性中,無法設置顯示行號,為了達到如圖所示的效果:

可以在DataGirdView的RowPostPaint事件中進行繪制。
如:添加以下方法代碼
private void DrawRowIndex(object sender, DataGridViewRowPostPaintEventArgs e)
??????? {
??????????? Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
?????????????? e.RowBounds.Location.Y,
?????????????? this.costomerDataGridView.RowHeadersWidth - 4,
?????????????? e.RowBounds.Height);
??????????? TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
??????????????? this.costomerDataGridView.RowHeadersDefaultCellStyle.Font,
??????????????? rectangle,
??????????????? this.costomerDataGridView.RowHeadersDefaultCellStyle.ForeColor,
??????????????? TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
??????? }
即可完成顯示行號的功能。