方法一:

网上最常见的做法是用DataGridView的RowPostPaint事件在RowHeaderCell中绘制行号:

private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            try
            {
                //添加行号 
                SolidBrush v_SolidBrush = new SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor);
                int v_LineNo = 0;
                v_LineNo = e.RowIndex + 1;

string v_Line = v_LineNo.ToString();

e.Graphics.DrawString(v_Line, e.InheritedRowStyle.Font, v_SolidBrush, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + 5);

}
            catch (Exception ex)
            {
                MessageBox.Show("添加行号时发生错误,错误信息:" + ex.Message, "操作失败");
            }
        }

但是这种方法在大数据量的时候性能比较差,每次滚动数据都会触发RowPostPaint事件。

方法二:

我的做法是给每行的HeaderCell赋值。

在网上发现有人提到这种做法,但是因为最后的显示问题而选择了上面的方法。具体问题就是,在行号超过2位,如100、1000,在选中该行时,DataGridView的行指示符▶会把行号往右挤,导致现实不全,100的时候显示▶ 10。

其实还是RowsHeaderWidth的大小有问题,将该列的宽度放大,行号显示的也没问题!

不知道他们有没有试过,上面绘制行号的方法在大行号的情况下显示也会有问题。

既然知道问题所在就要找到相应的解决方法。

具体做法是将DataGridView的RowsHeaderWidthSizeMode属性 设置为AutoSizeToAllHeaders或者AutoSizeToDisplayedHeaders,这样自动设置宽度就不会出现行指示符挤压行号的情况了。

对于每次DataGridView的行变化,我们都去更新行号,用RowsAdded和RowsRemoved事件。

代码如下:

private void DataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            for (int i = 0; i < e.RowCount; i++)
            {
                this.DataGridView1.Rows[e.RowIndex + i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
                this.DataGridView1.Rows[e.RowIndex + i].HeaderCell.Value = (e.RowIndex + i + 1).ToString();
            }
            for (int i = e.RowIndex + e.RowCount; i < this.DataGridView1.Rows.Count; i++)
            {
                this.DataGridView1.Rows[i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
                this.DataGridView1.Rows[i].HeaderCell.Value = (i + 1).ToString();
            }
        }

private void DataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            for (int i = 0; i < e.RowCount; i++)
            {
                this.DataGridView1.Rows[e.RowIndex + i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
                this.DataGridView1.Rows[e.RowIndex + i].HeaderCell.Value = (e.RowIndex + i + 1).ToString();
            }
            for (int i = e.RowIndex + e.RowCount; i < this.DataGridView1.Rows.Count; i++)
            {
                this.DataGridView1.Rows[i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
                this.DataGridView1.Rows[i].HeaderCell.Value = (i + 1).ToString();
            }
        }

DataGridView显示行号的几种方法来自http://www.soaspx.com/dotnet/csharp/csharp_20100204_2740.html的更多相关文章

  1. C# DataGridView显示行号的三种方法

    方法一: 网上最常见的做法是用DataGridView的RowPostPaint事件在RowHeaderCell中绘制行号: private void dgGrid_RowPostPaint( obj ...

  2. datalist、repearter、gridview显示行号的三种方法 或者是获取datalist行id

    原文发布时间为:2009-05-06 -- 来源于本人的百度文章 [由搬家工具导入] 1. if you are using SQL Server, try select identity(int,1 ...

  3. Notepad++去除代码行号的几种方法

    Notepad++去除代码行号的几种方法 (转自:http://hi.baidu.com/beer_zh/item/e70119309ee587f2a8842892)问:在网页中复制代码时,常常遇到高 ...

  4. Winform中的DatagridView显示行号

    1.设置 RowPostPaint 为true 2.启用RowPostPaint事件 /// <summary> /// DataGridView显示行号 /// </summary ...

  5. DataGridView显示行号

    //可以在DataGirdView的RowPostPaint事件中进行绘制. //如:添加以下方法代码 private void DrawRowIndex(object sender, DataGri ...

  6. Android Studio的使用(一)--显示行号、快速查找方法源

    1.显示行号,只需要右击编辑窗体的边界就可以了.(这种方法只能临时显示,下次打开文件就没了,对其他文件也没用). 2.永久显示行号 3.查找某个变量.类.方法定义的源头,同时可以查找布局文件,资源文件 ...

  7. DataGridView显示行号-RowPostPaint

    DataGridView控件在显示数据时,我们有时候需要显示行号,以便检索查看方便使用. 但DataGridView默认没有设置显示行号的属性. 此时我们只要在DataGridView的RowPost ...

  8. 【转】DataGridView显示行号

    ref:http://blog.csdn.net/xieyufei/article/details/9769631 方法一: 网上最常见的做法是用DataGridView的RowPostPaint事件 ...

  9. 让DataGridView显示行号

          http://www.cnblogs.com/JuneZhang/archive/2011/11/21/2257630.html 为了表示行号,我们可以在DataGridView的RowP ...

随机推荐

  1. [iOS微博项目 - 3.0] - 手动刷新微博

    github: https://github.com/hellovoidworld/HVWWeibo   A.下拉刷新微博 1.需求 在“首页”界面,下拉到一定距离的时候刷新微博数据 刷新数据的时候使 ...

  2. python 加密解密(base64, AES)

    1. 使用base64 s1 = base64.encodestring('hello world') s2 = base64.decodestring(s1) print s1, s2 结果 1 2 ...

  3. Head First设计模式-单例模式

    一.整体代码 Singleton.java public class Singleton { private static Singleton uniqueInstance; // other use ...

  4. JedisPoolConfig配置

      JedisPoolConfig config = new JedisPoolConfig();   //连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true config. ...

  5. Chrysler -- CCD (Chrysler Collision Detection) Data Bus

    http://articles.mopar1973man.com/general-cummins/34-engine-system/81-ccd-data-bus CCD (Chrysler Coll ...

  6. registerClassAlias()函数和getClassByAlias()函数

    flash.net 包中包含包级函数,可用于打开新的浏览器窗口,向服务器发送 URL 请求以及处理类别名. registerClassAlias()函数 public function registe ...

  7. Java Web模块——验证码模块

    一.什么是验证码及它的作用 验 证码为全自动区分计算机和人类的图灵测试的缩写,是一种区分用户是计算机的公共全自动程序,这个问题可以由计算机生成并评判,但是必须只有人类才能解答. 可以防止恶意破解密码. ...

  8. 浏览器禁用Cookie,基于Cookie的会话跟踪机制失效的解决的方法

    当浏览器禁用Cookies时.基于Cookie的会话跟踪机制就会失效.解决的方法是利用URL重写机制跟踪用户会话. 在使用URL重写机制的时候须要注意.为了保证会话跟踪的正确性,全部的链接和重定向语句 ...

  9. Python 初学笔记(转)

    >>> print "isn't that grand"isn't that grand #不需要转义的#为了让文字符扩展到多行,可以在一行的末尾使用反斜线符号, ...

  10. ip协议的数据分片备忘

    总结: 不仅tcp协议能对数据段进行分割,ip协议也具备这个功能,之所以会这样是两者都受到底层MTU的限制(虽说tcp是根据MSS限制来分割数据包,由于MTU=tcp包头+ip包头+MSS,所以其实也 ...