Categories
Asp, Asp.net

How to show row number in Gridview, Repeater,ListView, FormView, DetailsView and DataList

How to show row number in Gridview, Repeater,ListView, FormView, DetailsView and DataList

In this tutorial we will learn how to show Row_Number() to display row number in my data bound controls from long time. Every thing is working fine unless I have started using LINQ and Entity Framework. I found that Row_Number() is not supported. So I figured out anothor way to display row numbers in Asp .Net data bound controls. Hope it helps some one.

GridView:

<asp:templatefield>
<headertemplate>
Sno
</headertemplate>
<itemtemplate>
<%#(Container.DataItemIndex+1)%>
</itemtemplate>
</asp:templatefield>

ListView:

<asp:listview id="ListView1" runat="server">
    <itemtemplate>
        <%#Container.DataItemIndex+1 %>
    </itemtemplate>
</asp:listview>

FormView:

<asp:formview id="FormView1" runat="server">
    <itemtemplate>
        <%#Container.DataItemIndex+1 %>
    </itemtemplate>
</asp:formview>

Details View:

<asp:detailsview id="DetailsView1" runat="server" height="100px" width="100px">
    <fields>
        <asp:templatefield>
            <itemtemplate>
                <%#Container.DataItemIndex+1 %>
            </itemtemplate>
        </asp:templatefield>
    </fields>
</asp:detailsview>

DataList:

<asp:datalist id="DataList1" runat="server">
    <itemtemplate>
        <%#Container.ItemIndex+1 %>
    </itemtemplate>
</asp:datalist>

Repeater:

<asp:repeater id="Repeater1" runat="server">
    <itemtemplate>
        <%#Container.ItemIndex+1 %>
    </itemtemplate>
</asp:repeater>