我的一亩三分地 我就喜欢!
13fen  设为主页
 收藏本站
 
当前位置: > 一亩三分地:首页 > 网络学院 > 网络编程 > ASP专区 > Asp数据库/打印 > 如何让DataGrid自动生成序号
热门文章排行
热门文章排行 ASP中access数据库的路径问题(03-23)
我是否可以将Excel数据导入?(03-23)
访问数据库(2)(03-23)
asp.net中读取数据库的两种方式(03-23)
VB/ASP数据库连接字符串示例(03-23)
精采文章排行
精采文章排行
技术专题推荐
网管论坛交流
 

如何让DataGrid自动生成序号 

作者:佚名   来源:一亩三分地   点击:   日期:2007-03-23

在DataGrid的第一列自动生成序号,如下图:

    代码实现:

前台(WebForm1.aspx):

<asp:DataGrid id="grdCustomer" style="Z-INDEX: 102; LEFT: 30px; POSITION: absolute; TOP: 152px" runat="server" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="3" Font-Size="X-Small" AutoGenerateColumns="False">

<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#669999">

</SelectedItemStyle>

<AlternatingItemStyle BackColor="#FFF2F2"></AlternatingItemStyle>

<ItemStyle ForeColor="#000066" BackColor="#FAFFF9"></ItemStyle>

<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#006699"></HeaderStyle>

<FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>

<Columns>

<asp:TemplateColumn HeaderText="序号">

<ItemTemplate>

<asp:label id="lable1" runat=server><%#GetCount()%></asp:label>

</ItemTemplate>

</asp:TemplateColumn>

<asp:BoundColumn DataField="CustomerID" HeaderText="客户ID"></asp:BoundColumn>

<asp:BoundColumn DataField="CompanyName" HeaderText="公司名称"></asp:BoundColumn>

<asp:BoundColumn DataField="City" HeaderText="城市"></asp:BoundColumn>

<asp:BoundColumn DataField="Address" HeaderText="地址"></asp:BoundColumn>

</Columns>

</asp:DataGrid>

 
 

后台(WebForm1.aspx.cs):

int count;

private void Page_Load(object sender, System.EventArgs e)

{

     if(!IsPostBack)

     {

SqlConnection cnn = new SqlConnection();

cnn.ConnectionString = "data source=localhost;initial catalog=Northwind;password=;"

         +"persist security info=True;user id=sa;workstation id=APJ062;packet size=4096";

          string sqlstr = "select Top 10 CustomerID, CompanyName, City, Address from Customers";

         cnn.Open();

         SqlDataAdapter ad = new SqlDataAdapter(sqlstr,cnn);

         DataTable dt = new DataTable();

         ad.Fill(dt);

grdCustomer.DataSource = dt;

         grdCustomer.DataBind();

     }

//自动记数函数,在前台调用

public int GetCount()

{

     return ++count ;

}



文章评论】 【收藏本文】 【推荐好友】 【打印本文】 【论坛讨论

   相关文章:
·ASP中巧用Response属性 ·第六课:ASP脚本循环语句
·在 Web 页上使用条件数值格式 ·连接数据库查询手册(不仅仅适用于asp)
·警惕"给你的FileSystemObject对象加把锁" ·用ASP做全文检索

   文章评论:(条)
  
 请留名: 匿名评论   点击查看所有评论 网管论坛
 

  责任编辑:一分  声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。