datagridview如何在前面增加一列序号

ji_vicky 2007-07-04 02:45:41
我通过SQL的select进行查询,查询出来的内容设成一个datatable
我的datagridview的datasource连接这个datatable
我现在需要在DataGridView的最前面加一列序号, 是自动从1开始编码的,请问我需要如何完善这个功能呢?
...全文
1382 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wdsa 2011-05-10
  • 打赏
  • 举报
回复
DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
{
column.HeaderText = "flag";
column.Name = "flag";

column.CellTemplate = new DataGridViewCheckBoxCell(false);
column.TrueValue = 1;
column.FalseValue = 0;
column.Width = 25;
column.IndeterminateValue = 1;
this.dataGridView1.Columns.AddRange(new DataGridViewColumn[] {column});
}
ji_vicky 2007-07-04
  • 打赏
  • 举报
回复
thanks for lxcnn(过客)!
I try to do it by you method and over it!
swing05 2007-07-04
  • 打赏
  • 举报
回复
winform?
playzxw 2007-07-04
  • 打赏
  • 举报
回复
HeaderText="NO."
-过客- 2007-07-04
  • 打赏
  • 举报
回复
假设你原来取出的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;
ClsData 2007-07-04
  • 打赏
  • 举报
回复
前台
<asp:BoundField HeaderText="NO." ReadOnly="True">
<ItemStyle Width="5%" HorizontalAlign="Center" />
<HeaderStyle Height="30px" />
</asp:BoundField>

这是第二列,第一列示隐藏的id
ClsData 2007-07-04
  • 打赏
  • 举报
回复
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();
}

}
ji_vicky 2007-07-04
  • 打赏
  • 举报
回复
to :zhqs1000(子鱼)
我的不是vs2003而是2005
zhqs1000 2007-07-04
  • 打赏
  • 举报
回复
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{ if (e.Row.RowIndex >= 0)
{
e.Row.Cells[0].Text = Convert.ToString(e.Row.DataItemIndex + 1);
}
}

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧