gridview中第一列想实现1,2,3....序号方式应怎么办

解决方案 »

  1.   

    双击GridView的OnRowDataBound事件; 
    在后台的GridView1_RowDataBound()方法添加代码,最后代码如下所示: 
        protected void GridView1_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[0].Text = id.ToString(); 
            }     } 
    注意这时最好把前台的第一列的表头该为“编号”,因为以前的第一列被“吃掉”了。
      

  2.   

       <asp:GridView ID="GridView1" runat="server">
            <Columns>
            <asp:TemplateField>
            <HeaderTemplate>
              序号
            </HeaderTemplate>
            <ItemTemplate>
            <%#Container.DataItemIndex+1 %>
            </ItemTemplate>
            </asp:TemplateField>
            
            </Columns>
            </asp:GridView>
      

  3.   

    <asp:TemplateColumn>
            <ItemTemplate>
                 <%# Container.RowIndex+1 %>
             </ItemTemplate>
    </asp:TemplateColumn>
      

  4.   

    <asp:TemplateField HeaderText="编号">
    <itemtemplate><%# Container.DataItemIndex + 1%></itemtemplate>
    <itemstyle horizontalalign="Center" width="80px" />
    </asp:TemplateField>
    试试
      

  5.   

    编辑 定义一个lable  Text='<%# Container.ItemIndex%>
      

  6.   

    <asp:GridView ID="gridList" runat="server" PageSize="5" AutoGenerateColumns="false" AllowPaging="true">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate> <%#((GridViewRow)Container).RowIndex + 1 + gridList.PageIndex * gridList.PageSize%> </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="...." />
            <asp:BoundField DataField="...." />
        </Columns>
        </asp:GridView> 
      

  7.   

    GridView中:
    <%#Eval(Container.dataitemindex)%>Reapter中:
    <%#Eval(Container.itemindex)%>
      

  8.   

    自定义列在rowdatabound的时候手动填?或者在数据源里用临时表加个标识列,但这种方法对排序后仍要求自定义增长列的做法不适合。
      

  9.   

    <%#Container.DataItemIndex+1 %> 
      

  10.   

    唉   这在DataGrid还是正解   在GridView上可就不一样了