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. mysql online ddl2

          大家知道,互联网业务是典型的OLTP(online transaction process)应用,这种应用访问数据库的特点是大量的短事务高并发运行.因此任何限制高并发的动作都是不可接受的, ...

  2. 基于Qt的开源音乐播放器(CZPlayer)

    CZPlayer CZPlayer是基于Qt开发的一款功能强大的音乐播放器,该播放器的论坛地址请点击here,目前CZPlayer已经是第四个版本了,历史版本也分别在我的github上, github ...

  3. Tomcat配置随笔

    启动内存参数的配置 tomcat/bin/catalina.bat 如果是linux 就是 catalina.sh 在rem 的后面增加如下参数 set JAVA_OPTS= -Xms256m -Xm ...

  4. 加载程序到android虚拟机报错: android.widget.RelativeLayout cannot be cast to android.widget.Button

    05-23 02:53:48.416: E/Trace(875): error opening trace file: No such file or directory (2) 05-23 02:5 ...

  5. android 基础项目及开发出现:error opening trace file: No such file or directory错误解决

    本身这个错误不影响运行,但是看着烦啊.解决方案几种如下: 1.xml标签不完整或者未关闭,常有的事.Eclipse并不是所有的xml标记都检查,单双标记什么的. 2.有人说,据说是 android a ...

  6. ES各种错误解决

    _update API 执行报错 错误信息: { "error": { "root_cause": [ { "type": "re ...

  7. LeetCode 3

    Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...

  8. MySQL中的保留字

    本文转载自:http://www.cnblogs.com/lawdong/archive/2010/08/08/2357903.html ADD ALL ALTER ANALYZE AND AS AS ...

  9. 【置换,推理】UVa 1315 - Creaz tea party

    Dsecription n participants of «crazy tea party» sit around the table. Each minute one pair of neighb ...

  10. android 代码设置、打开wifi热点及热点的连接

    用过快牙的朋友应该知道它们在两天设备之间传输文件的时候使用的是wifi热点,然后另一台便连接这个热点再进行传输.快牙传输速度惊人应该跟它的这种机制有关系吧.不知道它的搜索机制是怎样的,但我想应该可以通 ...