在做一个东西中用到了DataGridView,并且要显示行号及其他操作(在后面的随笔中再添加吧,还没写好呢),所以,在网上搜索了相关资料,小结如下:
方法一:
我们可以在DataGridView的RowPostPaint事件中进行绘制。RowPostPaint事件,具体可以参照MSDN。
下面是实现代码:
private void Form1_Load(object sender, System.EventArgs e) { dataGridView1.Dock = DockStyle.Fill; dataGridView1.DataSource = System.Drawing.Imaging.ImageCodecInfo.GetImageDecoders(); } private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dataGridView1.RowHeadersWidth - 4, e.RowBounds.Height); TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dataGridView1.RowHeadersDefaultCellStyle.Font, rectangle, dataGridView1.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right); }
2、RowPostPaint
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事件。
代码如下:
privatevoidDataGridView1_RowsAdded(objectsender, DataGridViewRowsAddedEventArgs e) { for(inti =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(inti =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(); } } privatevoidDataGridView1_RowsRemoved(objectsender, DataGridViewRowsRemovedEventArgs e) { for(inti =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(inti =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的详细介绍:
Lullaby's Blog : http://blog.csdn.net/fangxing80/article/details/1561011#A8
个人开发历程知识库 : http://www.cnblogs.com/peterzb/archive/2009/05/29/1491891.html
感谢以上两位作者的辛勤劳动!