在GridView开头插入自动编号的方法
网上看了很多方法,发现都是照抄别人,而且,都是把第一列替换掉了,往往不是我们的理想结果。经过本人的实践,下面方法觉得更好用一些。就是不知道数据量过大时,效率怎么样,不过既然能用,就记下来吧,毕竟好记性不如Ctrl+V。
protected void myGridView_RowCreated(object sender, GridViewRowEventArgs e)
{
![](/Images/OutliningIndicators/InBlock.gif)
TableCellCollection tcHeader = e.Row.Cells;
TableCell newCell;
switch (e.Row.RowType)
{
case DataControlRowType.Header:
//第一行表头
newCell = new TableHeaderCell();
newCell.Text = "编号";
break;
case DataControlRowType.DataRow:
newCell = new TableCell();
newCell.Text = (e.Row.DataItemIndex + 1).ToString();
break;
default:
newCell = new TableCell();
break;
}
tcHeader.AddAt(0,newCell);
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)