让DataGridView显示行号
来源:互联网 发布:淘宝如何取消公益宝贝 编辑:程序博客网 时间:2024/05/01 13:27
DataGridView默认的属性中,不可设置显示行号。
效果如下:
为了表示行号,我们可以在DataGridView的RowPostPaint事件中进行绘制。
RowPostPaint事件,具体可以参照MSDN。
下面是实现代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
namespace DrawRowIndex
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.Dock = DockStyle.Fill;
dataGridView1.DataSource = ImageCodecInfo.GetImageDecoders();
}
private void 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);
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
namespace DrawRowIndex
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.Dock = DockStyle.Fill;
dataGridView1.DataSource = ImageCodecInfo.GetImageDecoders();
}
private void 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);
}
}
}
- 让DataGridView显示行号
- 让DataGridView显示行号
- 让DataGridView显示行号
- 让DataGridView显示行号
- 让DataGridView显示行号
- 让DataGridView显示行号
- 让DataGridView显示行号
- 让DataGridView显示行号,去除空记录行
- winForm中让DataGridView显示行号,去除空记录行
- DataGridView显示行号
- [转]DataGridView显示行号
- WinForm DataGridView 显示行号
- DataGridView显示行号
- dataGridView 显示行号
- datagridview中显示行号
- datagridview显示行号
- C# datagridview显示行号
- 在DataGridView中显示行号
- MySQL中MyISAM引擎与InnoDB引擎性能比较
- IP地址分类/IP地址10开头和172开头和192开头的区别/判断是否同一网段(A、B、C三类地址)
- dup dup2 open
- 该伙伴事务管理器已经禁止了它对远程/网络事务的支持
- 学习后的思考很难吗?
- 让DataGridView显示行号
- 穷人与富人的经典区别,穷人们要收藏啊!
- extaspnet 的panel如何动态改变iframeurl
- intel2011多线程编程大赛之一Game Of Life 最优寻址单线程
- ExtJs 处理复合主键 ID不唯一 数据缺失问题
- 汇编中的test和cmp指令[Z]
- Ubuntu 朗文不发音问题
- intel2011多线程编程大赛之一Game Of Life 最优寻址多线程
- 临时对象的隐蔽性与野指针不崩之谜