在DataGridView中显示行号

通过处理DataGridView的RowPostPaint事件来显示行号

 1          /**/ /// <summary>
 2        /// 在DataGridView中显示行号,需要要处理DataGridView的RowPostPaint事件
 3        /// 在datagridview的行标题单元格中绘制行号
 4        /// </summary>
 5        /// <param name="sender"></param>
 6        /// <param name="e"></param>

 7          private   void  dgvCmd_RowPostPaint( object  sender, DataGridViewRowPostPaintEventArgs e)
 8          {
 9
10            // Paint the row number on the row header.
11            // The using statement automatically disposes the brush.
12            DataGridView dgv = (DataGridView)sender;
13            using (SolidBrush b = new SolidBrush(dgv.RowHeadersDefaultCellStyle.ForeColor))
14            {
15                e.Graphics.DrawString(e.RowIndex.ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + dgv.RowHeadersWidth / 2, e.RowBounds.Location.Y + (e.RowBounds.Height - e.InheritedRowStyle.Font.GetHeight()) / 2);
16            }

17            //SolidBrush用于定义单色画笔。画笔用于填充图形形状,如:矩形、椭圆、扇型、多边型和封闭路径。此类无法继承。
18            //SolidBrush的构造函数:=new SolidBrush(Color color)
19            /**//*DrawString方法:DrawString(string s,Font font,Brush brush,float x,float y)
20                     s:要绘制的字符串
21                     f:定义字符串的文本格式
22             brush:确定所绘制的文本的颜色和纹理
23                     x:左上角的x坐标
24                     y:左上角的y坐标
25             */

26            //System.Globalization.CultureInfo.CurrentUICulture:提供区域性特定的格式设置信息
27        }

28
29

转载于:https://www.cnblogs.com/maozhh/archive/2007/09/24/904058.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值