• [导入]GridView中实现并列排名的例子


    Access数据库版本

    <%@ Page Language="C#" AutoEventWireup="true" Debug="true" %> <%@ Import Namespace="System.Data" %> <script runat="server"> public int TrapezoidIndex = 1; int LastNumer = 0; protected void Page_Load( object sender, EventArgs e ) { //ASPNET20Book.mdb数据库参见《ASP.NET 2.0应用开发技术》一书的光盘 string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\ASPNET20Book.mdb;Persist Security Info=True"; System.Data.OleDb.OleDbConnection cn = new System.Data.OleDb.OleDbConnection(ConnectionString); cn.Open(); string sql = "select * from [Score] Order BY Shuxue DESC"; System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand(sql, cn); System.Data.OleDb.OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); GridView1.DataSource = dr; GridView1.DataBind(); dr.Close(); cmd.Dispose(); cn.Dispose(); } protected void GridView1_RowCreated( object sender, GridViewRowEventArgs e ) { if (e.Row.RowType == DataControlRowType.DataRow) { System.Data.Common.DbDataRecord db = (System.Data.Common.DbDataRecord)e.Row.DataItem; int Shuxue = Int32.Parse(db["Shuxue"].ToString()); if (e.Row.RowIndex == 0) { LastNumer = Shuxue; } if (LastNumer != Shuxue) { TrapezoidIndex = e.Row.RowIndex + 1; } LastNumer = Shuxue; } } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>GridView并列排名的例子</title> </head> <body> <form id="form1" runat="server"> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCreated="GridView1_RowCreated"> <Columns> <asp:TemplateField HeaderText="数据序号"> <ItemTemplate> <%#Container.DataItemIndex + 1%> </itemtemplate> </asp:TemplateField> <asp:TemplateField HeaderText="学生姓名"> <ItemTemplate> <%#Eval("UserName")%> </itemtemplate> </asp:TemplateField> <asp:TemplateField HeaderText="数学"> <ItemTemplate> <%#Eval("Shuxue")%> </itemtemplate> </asp:TemplateField> <asp:TemplateField HeaderText="排名"> <ItemTemplate> <%#TrapezoidIndex%> </itemtemplate> </asp:TemplateField> </columns> </asp:GridView> </form> </body> </html>

    SQL Server数据库版本

    <%@ Page Language="C#" AutoEventWireup="true"%> <script runat="server"> public int TrapezoidIndex = 1; int LastNumer = 0; protected void Page_Load( object sender, EventArgs e ) { string ConnectionString = "Persist Security Info=False;User ID=sa;Password=;Initial Catalog=Book;Server=.;"; System.Data.SqlClient.SqlConnection cn = new System.Data.SqlClient.SqlConnection(ConnectionString); cn.Open(); string sql = "select *,(Yuwen + Shuxue + Yingyu) As TotalScore from [Score] Order BY TotalScore DESC"; System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, cn); System.Data.SqlClient.SqlDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection); GridView1.DataSource = dr; GridView1.DataBind(); dr.Close(); cmd.Dispose(); cn.Dispose(); } protected void GridView1_RowCreated( object sender, GridViewRowEventArgs e ) { if (e.Row.RowType == DataControlRowType.DataRow) { System.Data.Common.DbDataRecord db = (System.Data.Common.DbDataRecord)e.Row.DataItem; int TotalScore = Int32.Parse(db["TotalScore"].ToString()); if (e.Row.RowIndex == 0) { LastNumer = TotalScore; } if (LastNumer != TotalScore) { TrapezoidIndex = e.Row.RowIndex + 1; } LastNumer = TotalScore; } } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>GridView并列排名的例子</title> </head> <body> <form id="form1" runat="server"> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCreated="GridView1_RowCreated" Width="600px"> <Columns> <asp:TemplateField HeaderText="数据序号"> <ItemTemplate> <%#Container.DataItemIndex + 1%> </itemtemplate> </asp:TemplateField> <asp:TemplateField HeaderText="学生姓名"> <ItemTemplate> <%#Eval("UserName")%> </itemtemplate> </asp:TemplateField> <asp:TemplateField HeaderText="语文"> <ItemTemplate> <%#Eval("Yuwen")%> </itemtemplate> </asp:TemplateField> <asp:TemplateField HeaderText="数学"> <ItemTemplate> <%#Eval("Shuxue")%> </itemtemplate> </asp:TemplateField> <asp:TemplateField HeaderText="英语"> <ItemTemplate> <%#Eval("Yingyu")%> </itemtemplate> </asp:TemplateField> <asp:TemplateField HeaderText="总分"> <ItemStyle Font-Bold="true" ForeColor="red" /> <ItemTemplate> <%#Eval("TotalScore")%> </itemtemplate> </asp:TemplateField> <asp:TemplateField HeaderText="排名"> <ItemTemplate> <%#TrapezoidIndex%> </itemtemplate> </asp:TemplateField> </columns> </asp:GridView> </form> </body> </html>

    文章来源:http://dotnet.aspx.cc/article/7e82096a-b4f7-4012-b873-e1d51705e166/read.aspx
  • 相关阅读:
    单精度和双精度
    @Transactional注解用法
    JPA No EntityManager with actual transaction available for current thread
    上传文件Request Entity Too Large解决办法
    PG数据库查看当前会话和结束会话
    Chrome浏览器记不住密码也不提示保存密码win10
    ARM平台VMP保护开发入门
    关于我
    HDU7072:Boring data structure problem——题解
    HDU7067:Just another board game——题解
  • 原文地址:https://www.cnblogs.com/fansino/p/959333.html
Copyright © 2020-2023  润新知