博客
关于我
Winform dataGridView 添加自动编号
阅读量:510 次
发布时间:2019-03-07

本文共 788 字,大约阅读时间需要 2 分钟。

新建一个WinForm项目,在工具栏里拖一个dataGriView到窗体中,默认名称为dataGridView1,把数据源添加到dataGridView1中,运行,看到dataGriView1有数据显示,但没有行编号,所以我们需要添加一列,用来显示行号,以便我们知道这是第几条记录。选中dataGriView1,然后在属性列表的事件选择RowPostPaint事件,双击后添加事件处理函数,代码如下:

privatevoid dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e){    //自动编号,与数据无关    Rectangle rectangle = new 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);}

再次运行程序,就能看到多了一个标题列,显示数据的行号!

转载地址:http://excjz.baihongyu.com/

你可能感兴趣的文章
mysql客户端工具使用
查看>>
MySQL密码忘记,怎么办?
查看>>
mysql对同一张表进行查询和赋值更新
查看>>
mysql导入数据库出现:Incorrect string value: '\xE7\x82\xB9\xE9\x92\x9F' for column 'chinese' at row 1...
查看>>
mysql导入(ibd文件)
查看>>
Mysql工作笔记006---Mysql服务器磁盘爆满了_java.sql.SQLException: Error writing file ‘tmp/MYfXO41p‘
查看>>
mysql常用命令
查看>>
MySQL常用指令集
查看>>
mysql常用操作
查看>>
MySQL常用日期格式转换函数、字符串函数、聚合函数详
查看>>
MySQL常见错误分析与解决方法总结
查看>>
MySQL底层概述—2.InnoDB磁盘结构
查看>>
MySQL底层概述—3.InnoDB线程模型
查看>>
MySQL底层概述—5.InnoDB参数优化
查看>>
MySQL底层概述—6.索引原理
查看>>
MySQL底层概述—7.优化原则及慢查询
查看>>
MySQL底层概述—8.JOIN排序索引优化
查看>>
MySQL底层概述—9.ACID与事务
查看>>
Mysql建立中英文全文索引(mysql5.7以上)
查看>>
mysql建立索引的几大原则
查看>>