简体   繁体   中英

GridView Row Number using Markup (only)

I have a requirement to display row number in grid view. What is the best way to display the row number using BoundField or TemplateField?

Note: This need to be done using markup only (without code behind).

Note: When sorting happens, the row number should not be in sequence, the first row should go down with its content.

I have already referred the following:

  1. http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/2eead3e3-5cc2-40f7-a91c-8f7942d5329c/
<asp:TemplateField HeaderText="#" >
     <ItemTemplate>
     <%# Container.DataItemIndex + 1 %>
     </ItemTemplate>
</asp:TemplateField>

By the way, this solution proposed in article you referred. Why you don't like it and ask here?

The best place to do this would be to use the templatefield

 <asp:TemplateField HeaderText="Row Number"> <ItemTemplate> <%# Container.DataItemIndex + 1 %> </ItemTemplate> </asp:TemplateField> 

An examples http://www.devcurry.com/2010/01/add-row-number-to-gridview.html

The BoundField displays the value of specified DataSource field as text. The TemplateField allows to mix html or make use of web controls.

Please refer to the following explanation to confirm the difference. http://forums.asp.net/t/1804988.aspx/1

The gridview is rendered as html table. If you don't want to calculate the row number in code behind, you should use JQuery.

var rowCount = $('#myTable tr').length;

And you should fill the table footer with rowCount value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM