ref:http://blog.csdn.net/xieyufei/article/details/9769631

方法一:

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

 private void dgGrid_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
var grid = sender as DataGridView;
var rowIdx = (e.RowIndex + ).ToString(); var centerFormat = new StringFormat()
{
// right alignment might actually make more sense for numbers
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
}; var headerBounds = new Rectangle(e.RowBounds.Left, e.RowBounds.Top, grid.RowHeadersWidth, e.RowBounds.Height);
e.Graphics.DrawString(rowIdx, this.Font, SystemBrushes.ControlText, headerBounds, centerFormat);
}

但是这种方法在大数据量的时候性能比较差,每次滚动数据都会触发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 = ; i < e.RowCount; i++)
{
dataGridView1.Rows[e.RowIndex + i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
dataGridView1.Rows[e.RowIndex + i].HeaderCell.Value = (e.RowIndex + i + ).ToString();
}
for (int i = e.RowIndex + e.RowCount; i < this.dataGridView1.Rows.Count; i++)
{
dataGridView1.Rows[i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
dataGridView1.Rows[i].HeaderCell.Value = (i + ).ToString();
}
} private void dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
{
for (int i = ; i < e.RowCount; i++)
{
dataGridView1.Rows[e.RowIndex + i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
dataGridView1.Rows[e.RowIndex + i].HeaderCell.Value = (e.RowIndex + i + ).ToString();
} for (int i = e.RowIndex + e.RowCount; i < this.dataGridView1.Rows.Count; i++)
{
dataGridView1.Rows[i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
dataGridView1.Rows[i].HeaderCell.Value = (i + ).ToString();
}
}
方法三:
对于ReadOnly的Display,有更为简便的方法
private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
{
e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + );
}

【转】DataGridView显示行号的更多相关文章

  1. Winform中的DatagridView显示行号

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

  2. DataGridView显示行号-RowPostPaint

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

  3. DataGridView显示行号

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

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

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

  5. DataGridView显示行号的几种方法来自http://www.soaspx.com/dotnet/csharp/csharp_20100204_2740.html

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

  6. 让DataGridView显示行号

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

  7. DataGridView 显示行号与背景颜色

    实现的方式有好几种.之前使用的是下面这种在RowPostPaint事件中实现,效率不高.每次改变控件尺寸时都会执行 private void MsgGridView_RowPostPaint(obje ...

  8. DataGridView大扩展——显示行号

    原文 DataGridView大扩展——显示行号 在DataGridView 的实际使用中,经常需要标示出行号,这样可以比较醒目地看到当前信息.不过DataGridView 在绘制 DataGridV ...

  9. DataGridView自动行号

    最近又用了一下DataGridView控件,需要显示行号,我们知道在.net中DataGridView控件默认是不显示行号(数据的记录行数)的,后来通过查资料发现可以在DataGridView控件的R ...

随机推荐

  1. yii2 rbac 设计

    tbl_auth_item 根据type存储认证项目...role.task.operation 游客... 不能操作任何模块 普通用户 ..biz_rule..需要登录 只有这里返回true,才能进 ...

  2. XtraBackup原理3

    http://mysql.taobao.org/monthly/2016/03/07/ MySQL · 物理备份 · Percona XtraBackup 备份原理 前言 Percona XtraBa ...

  3. UNIX基础知识之输入和输出

    一.文件描述符 文件描述符(file descriptor)通常是一个小的非负整数,内核用它标识一个特定进程正在访问的文件.当内核打开一个已有文件或创建一个新文件时,它返回一个文件描述符.在读.写文件 ...

  4. A Simple Actions Recognition System

    1. Problem Definition There's no doubt that researches and applications on the foundation of videos ...

  5. Linux下使用mke2fsk格式化虚拟磁盘分区的方法

    原文地址:http://www.2cto.com/os/201202/119963.html 我们用dd命令就可以创建一个raw格式的虚拟磁盘,通常Xen就是使用这种格式的虚拟磁盘,今天就来讨论下怎样 ...

  6. 琐碎-同步centos集群的时间

    想马上上手HBase,其对集群时间同步要求很高,当然,hadoop也是

  7. [iOS 10 day by day] Day 1:开发 iMessage 的第三方插件

    本文介绍了 iOS 10 的一个重要更新:Messages 应用支持第三方插件了.作者用一个小游戏作为例子,说明了插件开发从建工程开始,到绘制界面.收发消息的全过程. <iOS 10 day b ...

  8. Customer reviews on Lexia3 V48 diagnostic tool in EOBD2.FR

    Robert said: Ok, so I bought a Lexia3 interface from EOBD2.FR in 2010. I have had no issues over the ...

  9. poj1753解题报告(枚举、组合数)

    POJ 1753,题目链接http://poj.org/problem?id=1753 题意: 有4*4的正方形,每个格子要么是黑色,要么是白色,当把一个格子的颜色改变(黑->白或者白-> ...

  10. Android(java)学习笔记105:Map集合的遍历之键值对对象找键和值

    package cn.itcast_01; import java.util.HashMap; import java.util.Map; import java.util.Set; /* * Map ...