1 // Select a DataGridView RowPostPaint generating method 2 . 3 . 4 . 5 Private void dgvStudentList_RowPostPaint ( Object SENDER, DataGridViewRowPostPaintEventArgs E) . 6 { . 7 DataGridViewStyle.DgvRowPostPaint ( the this .dgvStudentList, E) . 8 } . 9 10 . 11 12 is 13 is /// <Summary> 14 /// to add DataGridView line number 15 /// </ Summary> 16 /// <param name = "DGV"> </ param> . 17 /// <param name = "E"> </ param> 18 public static void DgvRowPostPaint(DataGridView dgv, DataGridViewRowPostPaintEventArgs e) 19 { 20 try 21 { 22 //添加行号 23 SolidBrush v_SolidBrush = new SolidBrush(dgv.RowHeadersDefaultCellStyle.ForeColor); 24 int v_LineNo = 0; 25 v_LineNo = e.RowIndex + 1; 26 string v_Line = v_LineNo.ToString(); 27 e.Graphics.DrawString (v_Line, e.InheritedRowStyle.Font, v_SolidBrush, e.RowBounds.Location.X + 15 , + e.RowBounds.Location.Y . 5 ); 28 } 29 the catch (Exception EX) 30 { 31 is the MessageBox. show ( " error, an error message occurs when adding the line number: " + ex.Message, " operation failed " ); 32 } 33 }