日期:2014-05-19  浏览次数:21170 次

datagridview如何在前面增加一列序号
我通过SQL的select进行查询,查询出来的内容设成一个datatable
我的datagridview的datasource连接这个datatable
我现在需要在DataGridView的最前面加一列序号,   是自动从1开始编码的,请问我需要如何完善这个功能呢?

------解决方案--------------------
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{ if (e.Row.RowIndex > = 0)
{
e.Row.Cells[0].Text = Convert.ToString(e.Row.DataItemIndex + 1);
}
}

------解决方案--------------------
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowIndex != -1)
{
int id = e.Row.RowIndex + 1;
e.Row.Cells[1].Text = id.ToString();
}

}
------解决方案--------------------
前台
<asp:BoundField HeaderText= "NO. " ReadOnly= "True ">
<ItemStyle Width= "5% " HorizontalAlign= "Center " />
<HeaderStyle Height= "30px " />
</asp:BoundField>

这是第二列,第一列示隐藏的id
------解决方案--------------------
假设你原来取出的DataTable为dt

DataTable ndt = new DataTable();
DataColumn dc = new DataColumn();
dc.ColumnName = "序号 ";
dc.AutoIncrement = true;
dc.AutoIncrementSeed = 1;
dc.AutoIncrementStep = 1;
ndt.Columns.Add(dc);
ndt.Merge(dt);

dataGridView1.DataSource = ndt;
------解决方案--------------------
HeaderText= "NO. "
------解决方案--------------------
winform?