简体   繁体   English

使用标记的GridView行号(仅限)

[英]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? 使用BoundField或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/ 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 这样做的最佳位置是使用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 一个例子http://www.devcurry.com/2010/01/add-row-number-to-gridview.html

The BoundField displays the value of specified DataSource field as text. BoundField将指定的DataSource字段的值显示为文本。 The TemplateField allows to mix html or make use of web controls. TemplateField允许混合html或使用Web控件。

Please refer to the following explanation to confirm the difference. 请参阅以下说明以确认差异。 http://forums.asp.net/t/1804988.aspx/1 http://forums.asp.net/t/1804988.aspx/1

The gridview is rendered as html table. gridview呈现为html表。 If you don't want to calculate the row number in code behind, you should use JQuery. 如果您不想计算后面代码中的行号,则应使用JQuery。

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

And you should fill the table footer with rowCount value. 你应该用rowCount值填充表格页脚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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