我要做个C#的datagridview控件,当向第一行输入数据的时候,自动增加的第二行的序号自动增加如:001,002?

如下图所示,急!

不要单独用一列做“序号”,把rowHead利用起来做你的“序号”(在行头显示行号的方法)

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-08-28
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
DataGridView temp = (DataGridView)sender;
using (SolidBrush b = new SolidBrush(temp.RowHeadersDefaultCellStyle.ForeColor))
{

if(e.RowIndex<10)
e.Graphics.DrawString(Convert.ToString("00"+e.RowIndex + 1, System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + 5);
if (e.RowIndex < 100&&e.RowIndex>10)
e.Graphics.DrawString(Convert.ToString("0" + e.RowIndex + 1, System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + 5);
if (e.RowIndex < 1000 && e.RowIndex > 100)
e.Graphics.DrawString(Convert.ToString(e.RowIndex + 1, System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + 5);

}
}
private void dataGridView1_Paint(object sender, PaintEventArgs e)
{
DataGridView temp = (DataGridView)sender;
using (SolidBrush b = new SolidBrush(temp.RowHeadersDefaultCellStyle.ForeColor))
{
e.Graphics.DrawString("序号", temp.Font, b, 8, 5);
}
}

你看着改动下吧,基本就是这样的本回答被提问者采纳