(转载)DataGridView显示行号
文章转自http://www.cnblogs.com/basicapp/archive/2006/10/07/522851.html
效果如下:
为了表示行号,我们可以在DataGridView的RowPostPaint事件中进行绘制。
RowPostPaint事件,具体可以参照MSDN。
下面是实现代码:
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);
}
}
}
public void view_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
{
if (e.RowHandle >= 0)
{
e.Info.DisplayText = (e.RowHandle + 1).ToString();
}
else if (e.RowHandle < 0 && e.RowHandle > -1000)
{
e.Info.DisplayText = "G" + e.RowHandle.ToString();
}
}
DataGridView显示行号
文章转自http://blog.sina.com.cn/s/blog_4c7757980100nejp.html
利用RowPostPaint事件,在DataGridView(例:dgvMaterial)每一行的开始显示行号。
1。设置DataGridView的RowHeadersVisible为true。(注:默认值即为true)
2。实现RowPostPaint事件。
private void dgvMaterial_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
//this method overrides the DataGridView's RowPostPaint event
//in order to automatically draw numbers on the row header cells
//and to automatically adjust the width of the column containing
//the row header cells so that it can accommodate the new row
//numbers,
//store a string representation of the row number in 'strRowNumber'
string strRowNumber = (e.RowIndex + 1).ToString();
//prepend leading zeros to the string if necessary to improve
//appearance. For example, if there are ten rows in the grid,
//row seven will be numbered as "07" instead of "7". Similarly, if
//there are 100 rows in the grid, row seven will be numbered as "007".
while (strRowNumber.Length < dgvMaterial.RowCount.ToString().Length) strRowNumber = "0" + strRowNumber;
//determine the display size of the row number string using
//the DataGridView's current font.
SizeF size = e.Graphics.MeasureString(strRowNumber, this.Font);
//adjust the width of the column that contains the row header cells
//if necessary
if (dgvMaterial.RowHeadersWidth < (int)(size.Width + 20)) dgvMaterial.RowHeadersWidth = (int)(size.Width + 20);
//this brush will be used to draw the row number string on the
//row header cell using the system's current ControlText color
Brush b = SystemBrushes.ControlText;
//draw the row number string on the current row header cell using
//the brush defined above and the DataGridView's default font
e.Graphics.DrawString(strRowNumber, this.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2));
}
*******************************************************************************
DataGridView添加行号
文章转自 http://jackyxfl.blog.163.com/blog/static/16413415020108241475694/
自己在做WINFORM小玩意的时候需要在datagridview上显示行号,上网搜索一下,找到如下代码,不错,先记录下来了。
在RowPostPaint事件中画出来
// 绘制行号
private void gvdata_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
try
{
Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
Convert.ToInt32(e.RowBounds.Location.Y + (e.RowBounds.Height - gvdata.RowHeadersDefaultCellStyle.Font.Size) / 2),
gvdata.RowHeadersWidth - 4, e.RowBounds.Height);
TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
gvdata.RowHeadersDefaultCellStyle.Font, rectangle, gvdata.RowHeadersDefaultCellStyle.ForeColor,
TextFormatFlags.Right);
}
catch (Exception ex)
{
Console.Write("dgv1_RowPostPaint:" + ex.Message);
}
}
也可以用如下方法:
void DataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
using (SolidBrush b = new SolidBrush(Color.Red))
{
e.Graphics.DrawString(Convert.ToString(e.RowIndex + 1),
e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
b.Dispose();
}
}
效果如图:
**************************************************************
DataGridView显示行号的几种方法
文章出自 http://www.byywee.com/page/M0/S231/231472.html
网上最常见的做法是用DataGridView的RowPostPaint事件在RowHeaderCell中绘制行号:
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
try
{
//添加行号
SolidBrush v_SolidBrush = new SolidBrush(dataGridView1.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, "操作失败");
}
}
但是这种方法在大数据量的时候性能比较差,每次滚动数据都会触发RowPostPaint事件。
方法二:
我的做法是给每行的HeaderCell赋值。
在网上发现有人提到这种做法,但是因为最后的显示问题而选择了上面的方法。具体问题就是,在行号超过2位,如100、1000,在选中该行时,DataGridView的行指示符?会把行号往右挤,导致现实不全,100的时候显示? 10。
其实还是RowsHeaderWidth的大小有问题,将该列的宽度放大,行号显示的也没问题!
不知道他们有没有试过,上面绘制行号的方法在大行号的情况下显示也会有问题。
既然知道问题所在就要找到相应的解决方法。
具体做法是将DataGridView的RowsHeaderWidthSizeMode属性设置为AutoSizeToAllHeaders或者AutoSizeToDisplayedHeaders,这样自动设置宽度就不会出现行指示符挤压行号的情况了。
对于每次DataGridView的行变化,我们都去更新行号,用RowsAdded和RowsRemoved事件。
RowsAdded & RowsRemoved
{
for (int i =0; i < e.RowCount; i++)
{
this.DataGridView1.Rows[e.RowIndex + i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
this.DataGridView1.Rows[e.RowIndex + i].HeaderCell.Value = (e.RowIndex + i +1).ToString();
}
for (int i = e.RowIndex + e.RowCount; i <this.DataGridView1.Rows.Count; i++)
{
this.DataGridView1.Rows[i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
this.DataGridView1.Rows[i].HeaderCell.Value = (i +1).ToString();
}
}
privatevoid DataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
{
for (int i =0; i < e.RowCount; i++)
{
this.DataGridView1.Rows[e.RowIndex + i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
this.DataGridView1.Rows[e.RowIndex + i].HeaderCell.Value = (e.RowIndex + i +1).ToString();
}
for (int i = e.RowIndex + e.RowCount; i <this.DataGridView1.Rows.Count; i++)
{
this.DataGridView1.Rows[i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
this.DataGridView1.Rows[i].HeaderCell.Value = (i +1).ToString();
}
}
privatevoid DataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
for (int i =0; i < e.RowCount; i++)
{
this.DataGridView1.Rows[e.RowIndex + i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
this.DataGridView1.Rows[e.RowIndex + i].HeaderCell.Value = (e.RowIndex + i +1).ToString();
}
for (int i = e.RowIndex + e.RowCount; i <this.DataGridView1.Rows.Count; i++)
{
this.DataGridView1.Rows[i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
this.DataGridView1.Rows[i].HeaderCell.Value = (i +1).ToString();
}
}
privatevoid DataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
{
for (int i =0; i < e.RowCount; i++)
{
this.DataGridView1.Rows[e.RowIndex + i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
this.DataGridView1.Rows[e.RowIndex + i].HeaderCell.Value = (e.RowIndex + i +1).ToString();
}
for (int i = e.RowIndex + e.RowCount; i <this.DataGridView1.Rows.Count; i++)
{
this.DataGridView1.Rows[i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
this.DataGridView1.Rows[i].HeaderCell.Value = (i +1).ToString();
}
}