博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#DataGridView添加序号
阅读量:7061 次
发布时间:2019-06-28

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

效果图:

 

一.Form添加全局变量

//这里的值是表格行头的初始宽度

int rowHeaderWidth = 41;

二.DataGridView添加事件

private void dgvReconcile_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)         {
ChangeRowHeaderWidth(); } private void dgvReconcile_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e) {
ChangeRowHeaderWidth(); } private void dgvReconcile_RowHeadersWidthChanged(object sender, EventArgs e) {
//这里是移动全选复选框的位置 int offw = dgvReconcile.RowHeadersWidth - rowHeaderWidth; cbSelectAll.Location = new Point(cbSelectAll.Location.X + offw, cbSelectAll.Location.Y); rowHeaderWidth = dgvReconcile.RowHeadersWidth; } private void dgvReconcile_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) {
if (dgvReconcile.Rows.Count > 0) {
using (SolidBrush b = new SolidBrush(e.RowIndex == dgvReconcile.CurrentRow.Index ? Color.Red : dgvReconcile.RowHeadersDefaultCellStyle.ForeColor)) {
e.Graphics.DrawString((Convert.ToInt32(e.RowIndex) + 1).ToString(System.Globalization.CultureInfo.CurrentCulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4); } } }
其中ChangeRowHeaderWidth方法代码:
///         ///计算表格序号列宽度         ///         private void ChangeRowHeaderWidth()         {
Graphics g = Graphics.FromHwnd(dgvReconcile.Handle); SizeF sf = g.MeasureString(dgvReconcile.Rows.Count.ToString(), dgvReconcile.Font); dgvReconcile.RowHeadersWidth = (int)Math.Ceiling(sf.Width + 20); }
 

转载于:https://www.cnblogs.com/zcfsky/archive/2012/02/17/2356390.html

你可能感兴趣的文章
Linq使用Group By经验总结
查看>>
Ajax原生写法
查看>>
无线遥控器方案 Si4010/Si4012
查看>>
Hulu面试题
查看>>
On iPad, UIImagePickerController must be presented via UIPopoverController
查看>>
项目管理沟通计划
查看>>
IntelliJ IDEA 12.1.4 解决中文乱码
查看>>
据说每个大牛、小牛都应该有自己的库——DOM处理
查看>>
第一章 初识MVC4
查看>>
red ant
查看>>
树的子结构
查看>>
ireport常见问题
查看>>
Advanced Awk for Sysadmins
查看>>
动态Pivot(2)
查看>>
用MATLAB实现字符串分割
查看>>
EntityFramework ,ef 介绍
查看>>
C#代码 json类
查看>>
九度 1254:N皇后问题
查看>>
解决php收邮件乱码问题
查看>>
thoughtbot/capybara-webkit
查看>>