DataGridView 如何产生序列号

DataGridView 如何产生序列号,第1张

代码写在 DataGridView的RowPostPaint事件

//DataGridView控件添加序号:事件RowPostPaint

private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)

{

DataGridView temp = (DataGridView)sender

using (SolidBrush b = new SolidBrush(temp.RowHeadersDefaultCellStyle.ForeColor))

{

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)

}

}

// DataGridView序号列的标题显示:序号 Paint 事件

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)

}

}

System.Data.DataTable table = new DataTable()

System.Data.DataColumn column = new DataColumn()

column.ColumnName = "序号"

column.AutoIncrement = true

column.AutoIncrementSeed = 1

column.AutoIncrementStep = 1

table.Columns.Add(column)

table.Merge(ds.Tables[0])

datagridview1.DataSource = table

datagridview1.Columns["序号"].DisplayIndex = 0//调整列顺序

复制过来的,希望对你有帮助,c# 支持 隐式的转换,你也可以用Convert 来转换类型

你也可以使用:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{if (e.Row.RowIndex >= 0)

{

e.Row.Cells[0].Text = Convert.ToString(e.Row.DataItemIndex + 1)

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11449494.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-16
下一篇2023-05-16

发表评论

登录后才能评论

评论列表(0条)

    保存