DataGridView显示行号
2012-07-13 12:27
281 查看
可以做成扩展控件,这里是主要代码:
方法一:
private void dataGridView2_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
using (var brush = new SolidBrush(dataGridView2.RowHeadersDefaultCellStyle.ForeColor))
{
e.Graphics.DrawString((e.RowIndex + 1).ToString(), dataGridView2.DefaultCellStyle.Font, brush, e.RowBounds.Location.X + 12, e.RowBounds.Y + 5);
}
}
方法二:
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == -1 && e.RowIndex >= 0 && e.RowIndex < dataGridView1.Rows.Count)
{
//dataGridView1.Rows[e.RowIndex].HeaderCell.Value = (e.RowIndex).ToString();
e.PaintBackground(e.ClipBounds, true);
e.Graphics.DrawString((e.RowIndex + 1).ToString(), Font, Brushes.Black, e.CellBounds.Left + 6,
e.CellBounds.Top + 5);
e.Handled = true;
}
}
方法三:继承DataGridView扩展为自定义控件
public partial class DataGridViewEx : DataGridView
{
bool showRowHeaderNumbers;
/// <summary>
/// 是否显示行号
/// </summary>
[Category("扩展属性"), Description("是否显示行号"), DefaultValue(false)]
public bool ShowRowHeaderNumbers
{
get { return showRowHeaderNumbers; }
set
{
if (showRowHeaderNumbers != value)
Invalidate();
showRowHeaderNumbers = value;
}
//get;
//set;
}
public DataGridViewEx()
{
InitializeComponent();
}
protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
{
if (ShowRowHeaderNumbers)
{
string title = (e.RowIndex + 1).ToString();
Brush brush = Brushes.Black;
e.Graphics.DrawString(title, DefaultCellStyle.Font, brush, e.RowBounds.Location.X + RowHeadersWidth / 2 - 4, e.RowBounds.Location.Y + 4);
}
base.OnRowPostPaint(e);
}
}
参考:/article/5965017.html
方法一:
private void dataGridView2_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
using (var brush = new SolidBrush(dataGridView2.RowHeadersDefaultCellStyle.ForeColor))
{
e.Graphics.DrawString((e.RowIndex + 1).ToString(), dataGridView2.DefaultCellStyle.Font, brush, e.RowBounds.Location.X + 12, e.RowBounds.Y + 5);
}
}
方法二:
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == -1 && e.RowIndex >= 0 && e.RowIndex < dataGridView1.Rows.Count)
{
//dataGridView1.Rows[e.RowIndex].HeaderCell.Value = (e.RowIndex).ToString();
e.PaintBackground(e.ClipBounds, true);
e.Graphics.DrawString((e.RowIndex + 1).ToString(), Font, Brushes.Black, e.CellBounds.Left + 6,
e.CellBounds.Top + 5);
e.Handled = true;
}
}
方法三:继承DataGridView扩展为自定义控件
public partial class DataGridViewEx : DataGridView
{
bool showRowHeaderNumbers;
/// <summary>
/// 是否显示行号
/// </summary>
[Category("扩展属性"), Description("是否显示行号"), DefaultValue(false)]
public bool ShowRowHeaderNumbers
{
get { return showRowHeaderNumbers; }
set
{
if (showRowHeaderNumbers != value)
Invalidate();
showRowHeaderNumbers = value;
}
//get;
//set;
}
public DataGridViewEx()
{
InitializeComponent();
}
protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
{
if (ShowRowHeaderNumbers)
{
string title = (e.RowIndex + 1).ToString();
Brush brush = Brushes.Black;
e.Graphics.DrawString(title, DefaultCellStyle.Font, brush, e.RowBounds.Location.X + RowHeadersWidth / 2 - 4, e.RowBounds.Location.Y + 4);
}
base.OnRowPostPaint(e);
}
}
参考:/article/5965017.html
相关文章推荐
- DataGridView显示行号的几种方法
- 为Winform程序中DataGridView控件增加自动显示行号功能
- C# DataGridView显示行号的三种方法
- datagridview显示行号
- DataGridView显示行号
- 在dataGridView中显示行号(VS2005)
- 让DataGridView显示行号,去除空记录行
- C# DataGridView显示行号的三种方法
- 【Winform】DataGridView在RowHead内显示行号
- winform让DataGridView显示行号
- C# DataGridView显示行号的两种方法
- 登山-C#-DataGridView显示行号
- DataGridView控件显示行号
- 让DataGridView显示行号
- 让DataGridView显示行号
- 在DataGridView中显示行号
- c#中dataGridView显示行号的方法
- C# winfrom中 DataGridView中显示行号
- dataGridView自动生成行号1234.....显示
- datagridview显示行号