1 //选中DataGridView的RowPostPaint生成方法 2 3 4 5 private void dgvStudentList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) 6 { 7 DataGridViewStyle.DgvRowPostPaint(this.dgvStudentList,e) 8 } 9 10 11 12 13 /// <summary> 14 /// 给DataGridView添加行号 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 catch (Exception ex) 30 { 31 MessageBox.Show("添加行号时发生错误,错误信息:" + ex.Message, "操作失败"); 32 } 33 }