下面是 DataGridView 新增行号封装的通用方法:
1 /// <summary> 2 /// 给DataGridView添加行号 3 /// </summary> 4 /// <param name="dgv"></param> 5 /// <param name="e"></param> 6 public static void DgvRowPostPaint(DataGridView dgv, DataGridViewRowPostPaintEventArgs e) 7 { 8 try 9 { 10 //添加行号 11 SolidBrush v_SolidBrush = new SolidBrush(dgv.RowHeadersDefaultCellStyle.ForeColor); 12 int v_LineNo = 0; 13 v_LineNo = e.RowIndex + 1; 14 string v_Line = v_LineNo.ToString(); 15 e.Graphics.DrawString(v_Line, e.InheritedRowStyle.Font, v_SolidBrush, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + 5); 16 } 17 catch (Exception ex) 18 { 19 MessageBox.Show("添加行号时发生错误,错误信息:" + ex.Message, "操作失败"); 20 } 21 }
方法调用:在需要添加的 DataGridView 里面选择 RowPostPaint 事件:
传入需要设置行号的控件名称即可
DgvRowPostPaint(this.DataGridView, e);
标签:Location,DataGridView,行号,添加,LineNo,SolidBrush,WinForm From: https://www.cnblogs.com/ywd7/p/17499355.html