




全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
自己在做WINFORM小玩意的时候需要在datagridview上显示行号,上网搜索一下,找到如下代码,不错,先记录下来了。 在RowPostPaint事件中画出来C#代码 1. /绘制行号 2. privatevoidgvdata_RowPostPaint(objectsender,DataGridViewRowPostPaintEventArgse) 3. 4. try 5. 6. 7. Rectanglerectangle=newRectangle(e.RowBounds.Location.X, 8. 9. Convert.ToInt32(e.RowBounds.Location.Y+(e.RowBounds.Height-gvdata.RowHeadersDefaultCellStyle.Font.Size)/2), 10. 11. gvdata.RowHeadersWidth-4,e.RowBounds.Height); 12. 13. TextRenderer.DrawText(e.Graphics,(e.RowIndex+1).ToString(), 14. 15. gvdata.RowHeadersDefaultCellStyle.Font,rectangle,gvdata.RowHeadersDefaultCellStyle.ForeColor, 16. 17. TextFormatFlags.Right); 18. 19. 20. 21. 22. 23. catch(Exceptionex) 24. 25. 26. Console.Write(dgv1_RowPostPaint:+ex.Message); 27. 28. 29. 效果如图:/* #region dataGridView /显示行号 private void dataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dataGridView.RowHeadersWidth - 8, e.RowBounds.Height); TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dataGridView.RowHeadersDefaultCellStyle.Font, rectangle, dataGridView.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right); #endregion private void dataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) try /添加行号 SolidBrush v_SolidBrush = new SolidBrush(dataGridView.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, 操作失败); */人云亦云,我为DataGridView控件显示行号正名 前些天在写个小程序,用到DataGridView,想给它动态的显示行号。不是很费劲GOOGLE了一下,这GOOGLE不要紧,发现了不少问题。以下基本上都是GOOGLE搜索出来的网上的一些解决方法,千篇一律都是这样的:private void DataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e) for (int i = 0; i e.RowCount; i+) this.dgvKBRollUp.Rowse.RowIndex + i.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight; this.dgvKBRollUp.Rowse.RowIndex + i.HeaderCell.Value = (e.RowIndex + i + 1).ToString(); for (int i = e.RowIndex + e.RowCount; i this.dgvKBRollUp.Rows.Count; i+) this.dgvKBRollUp.Rowsi.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight; this.dgvKBRollUp.Rowsi.HeaderCell.Value = (i + 1).ToString(); private void DataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e) for (int i = 0; i e.RowCount; i+) this.dgvKBRollUp.Rowse.RowIndex + i.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight; this.dgvKBRollUp.Rowse.RowIndex + i.HeaderCell.Value = (e.RowIndex + i + 1).ToString(); for (int i = e.RowIndex + e.RowCount; i this.dgvKBRollUp.Rows.Count; i+) this.dgvKBRollUp.Rowsi.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight; this.dgvKBRollUp.Rowsi.HeaderCell.Value = (i + 1).ToString(); 只要用过这段代码的人就应该发现这段代码是运行出错的。原因就出在RowsRemoved事件里,会抛出一个Index outof range的异常。然而就是这么一段有错的代码,几乎充斥着整个互联网,千篇一律的COPY,没有一个人纠正。先说下这段代码出错的原因吧:在RowsRemoved事件里,最开始生成DataGridView的数据的时候,也是会触发这个事件的。这个时候DataGridView控件的Rows.Count就是0。那下面这行代码就有问题了:this.dgvKBRollUp.Rowse.RowIndex + i.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight; e.RowIndex + i,这里对应的是Rows0,但是Rows.Count还是0啊,Rows0是不存在的。要存在Rows0起码DataGridView控件要有一行才行。为了避免这个错误,小小的修改代码就行了:private void dgvKBRollUp_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e) if (dgvKBRollUp.Rows.Count != 0) for (int i = 0; i e.RowCount; i+) this.dgvKBRollUp.Rowse.RowIndex + i.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight; this.dgvKBRollUp.Rowse.RowIndex + i.HeaderCell.Value = (e.RowIndex + i + 1).ToString(); for (int i = e.RowIndex + e.RowCount; i this.dgvKBRollUp.Rows.Count; i+) this.dgvKBRollUp.Rowsi.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight; this.dgvKBRollUp.Rowsi.HeaderCell.Value = (i + 1).ToString(); 只要加上一个对Rows.Count的判断就可以避免这个错误。希望网上的一些COPY的朋友也要注意了,以后COPY过来的时候,自己还是要动手验证一下。将一个错误的信息胡乱的传播是对一些新手以及自己都不怎么好的。最后附上微软MSDN里面关于e.RowIndex和e.RowCount的一段代码:System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder(); messageBoxCS.AppendFormat(0 = 1, RowIndex, e.RowIndex); messageBoxCS.AppendLin
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 面试即兴演讲试题及范例
- 2024血透治疗中的监护及护理要点试题及答案
- 新进员工岗前安全教育培训试题及答案
- 2025年新《公司法》知识竞赛题库(含答案)
- 2024年大学礼仪知识竞赛题库与答案
- 2024年公职人员考试时事政治考试题库(附答案)
- 北京户外徒步知识培训课件
- 2025年安全生产培训考试题及答案
- 标本采集顺序课件
- 2024海南省消防宣传月知识竞赛练习题及答案
- 农机购买销售合同(2025版)
- 硬质合金成型工专业知识考试题库含答案
- 视神经脊髓炎病例汇报
- 高质量数据集实践指南(1.0)
- 消除艾滋病、梅毒和乙肝母婴传播项目工作制度及流程(模板)
- 2025年机动车授权签字人考试题库及答案
- 玉米机收减损技术
- AI大模型数字港口业务架构总体设计方案
- 2025年高考全国一卷英语试题真题文档版(含答案)
- 白酒广告投放管理制度
- 实训室管理课件
评论
0/150
提交评论