欢迎光临
我们一直在努力

如何让DataGrid自动生成序号-ASP教程,数据库相关

建站超值云服务器,限时71元/月

在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 ;

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 如何让DataGrid自动生成序号-ASP教程,数据库相关
分享到: 更多 (0)