DataGridView添加行号

本文详细介绍了如何在DataGridView控件中添加行号,通过重写RowPostPaint事件,使用SolidBrush绘制行号到指定位置,确保了行号的正确显示。同时提供了异常处理机制,保证了程序的稳定性。
 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         }

 

转载于:https://www.cnblogs.com/leizhui/p/11505798.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值