Add an AutoNumber column to a DataGrid, such as a control

Source: Internet
Author: User
First, positive sequence
A, Allowpaging=false case
<asp:datagrid id= "DATAGRID1" runat= "Server" >
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<%# Container.itemindex + 1%>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

can be achieved
But the more interesting approach is to use this method
<asp:datagrid id= "DATAGRID1" runat= "Server" >
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<%# this. DataGrid1.Items.Count + 1%>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>


Some people may find it strange why items.count is like this, not all the sum. But it's easy to understand if you understand the binding process.
[From above it is binding in the ItemCreated event so the Items.Count is exactly the current ordinal number]
B, allowpaging= under "True"
If you have a DataGrid that supports paging, you can do the following
<asp:datagrid id= "DATAGRID1" runat= "Server" allowpaging= "True" >
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<%# this. Datagrid1.currentpageindex * this. Datagrid1.pagesize + Container.itemindex + 1%>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>


Second, the reverse method
Serial number
Content

4
Taye

3
BOx

2
Glass

1
StarCraft

By the above can know to use
This. Datagrid1.items.count-container.itemindex + 1 method is impossible to achieve, get the value and the plenary is 1
Paging is more the case. So we're going to get the number of rows from the data source at first.
. CS

private int rowscount = 0;
protected int Rowscount
{
get{return rowscount;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.