惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
博客园 - 【当耐特】
Microsoft Azure Blog
Microsoft Azure Blog
WordPress大学
WordPress大学
Stack Overflow Blog
Stack Overflow Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
Cloudbric
Cloudbric
The Register - Security
The Register - Security
小众软件
小众软件
PCI Perspectives
PCI Perspectives
G
Google Developers Blog
AI
AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google DeepMind News
Google DeepMind News
Google DeepMind News
Google DeepMind News
宝玉的分享
宝玉的分享
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
TaoSecurity Blog
TaoSecurity Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
F
Full Disclosure
N
Netflix TechBlog - Medium
博客园_首页
Last Week in AI
Last Week in AI
A
Arctic Wolf
B
Blog RSS Feed
J
Java Code Geeks
C
Cybersecurity and Infrastructure Security Agency CISA
I
InfoQ
aimingoo的专栏
aimingoo的专栏
云风的 BLOG
云风的 BLOG
NISL@THU
NISL@THU
MyScale Blog
MyScale Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Jina AI
Jina AI
有赞技术团队
有赞技术团队
S
Schneier on Security
L
Lohrmann on Cybersecurity
P
Privacy & Cybersecurity Law Blog
T
Threat Research - Cisco Blogs
P
Palo Alto Networks Blog
S
Security @ Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
Security Latest
Security Latest
Vercel News
Vercel News
博客园 - 司徒正美
Webroot Blog
Webroot Blog
Hacker News: Ask HN
Hacker News: Ask HN
A
About on SuperTechFans

博客园 - lrary

JavaScript经典技巧 - lrary - 博客园 动态添加ASP.NET控件并绑定处理事件一例 - lrary - 博客园 脚本收藏 - lrary - 博客园 一些sql语句的详细解释[转] 特殊的DataGrid的绑定 - lrary - 博客园 纯脚本搞掂DataGrid表表头不动,表身滚动。(转摘) - lrary - 博客园 检验密码强度的JS类 - lrary - 博客园 数据库设计规范 V2.0 SQL语句特---殊统计(1) DataGrid 多行 DataGrid怎么产生一个分类的题头 AJAX 在.net的应用 sql怎么使用外连接 Asp.NET程序中常用的三十三种代码 检测含有中文字符串的实际长度 根据区位得到汉字拼音首字母(c#) 认识ASP.NET配置文件Web.config Asp.net(C#)实现验证码功能 查找和统计页面上控件
给Repeater、Datalist和Datagrid增加自动编号列
lrary · 2006-05-06 · via 博客园 - lrary

给Repeater、Datalist和Datagrid增加自动编号列


内容
1
Taye
2
BOx
3
Glass
4
StarCraft

一、正序
A、AllowPaging=False情况下,使用以下方法就可以实现:

1<asp:DataGrid id="DataGrid1" runat="server">
2    <Columns>
3     <asp:TemplateColumn>
4      <ItemTemplate>
5       <%# Container.ItemIndex + 1%>
6      </ItemTemplate>
7     </asp:TemplateColumn>
8    </Columns>
9 </asp:DataGrid>


不过更有趣的方法是使用这个方法:

1<asp:DataGrid id="DataGrid1" runat="server">
2    <Columns>
3     <asp:TemplateColumn>
4      <ItemTemplate>
5       <%# this.DataGrid1.Items.Count + 1%>
6      </ItemTemplate>
7     </asp:TemplateColumn>
8    </Columns>
9</asp:DataGrid>


也许有些人会觉得很奇怪为什么Items.Count会这样,而不是出来全部总合,但如果你了解绑定的过程时就容易理解。[从上面来看就是在ItemCreated事件中进行绑定所以得到的Items.Count刚好是当前的序号]

B、AllowPaging="True"下,如果DataGrid支持分页则可以如下:

1<asp:DataGrid id="DataGrid1" runat="server" AllowPaging="True">
2    <Columns>
3     <asp:TemplateColumn>
4      <ItemTemplate>
5       <%# this.DataGrid1.CurrentPageIndex * this.DataGrid1.PageSize + Container.ItemIndex + 1%>
6      </ItemTemplate>
7     </asp:TemplateColumn>
8    </Columns>
9</asp:DataGrid>


二、倒序的方法
序号
内容
4
Taye
3
BOx
2
Glass
1
StarCraft

由上面可以知道使用this.DataGrid1.Items.Count - Container.ItemIndex + 1方法是不可能实现的,得到值而且全会为1,分页的情况下更是一样.所以一开始我们就要取得数据源的行数:

 1private int rowscount = 0;
 2         protected int RowsCount
 3         {
 4              getreturn rowscount;}
 5              setthis.rowscount = value; }
 6         }

 7     
 8         private void Page_Load(object sender, System.EventArgs e)
 9         {
10              // 在此处放置用户代码以初始化页面
11              if(!IsPostBack)
12                   this.BindData();
13         }

14         private void BindData()
15         {
16              SqlConnection cn = new SqlConnection("server=(local);database=NorthWind;uid=sa;pwd=");
17              string str=@"SELECT Employees.EmployeeID, Orders.EmployeeID
18                                 FROM Employees INNER JOIN
19                       Orders ON Employees.EmployeeID = Orders.EmployeeID ";
20              SqlDataAdapter sqlda = new SqlDataAdapter(str,cn);
21              DataSet ds = new DataSet();
22              sqlda.Fill(ds);
23              this.RowsCount = ds.Tables[0].Rows.Count;
24              this.DataGrid1.DataSource = ds;
25              this.DataGrid1.DataBind();
26}


1<asp:DataGrid id="DataGrid1" runat="server" AllowPaging="True">
2                            <Columns>
3                                   <asp:TemplateColumn>
4                                          <ItemTemplate>
5                                                 <%# RowsCount - DataGrid1.CurrentPageIndex * DataGrid1.PageSize - Container.ItemIndex %>
6                                          </ItemTemplate>
7                                   </asp:TemplateColumn>
8                            </Columns>
9                     </asp:DataGrid>