GridView中实现并列排名的例子 SQL Server版

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>

时间: 2024-09-19 10:45:08

GridView中实现并列排名的例子 SQL Server版的相关文章

GridView中实现并列排名的例子[access版]

<%@ Page Language="C#" AutoEventWireup="true" Debug="true" %> <%@ Import Namespace="System.Data" %> <script runat="server"> public int TrapezoidIndex = 1; int LastNumer = 0; protected voi

如何将云端SQL Azure中的数据库备份到本地SQL Server

SQL Azure提供一系列的数据复制和同步机制.例如,本地的SQL Server数据库可以被发布到SQL Azure上:SQL Azure的数据库也可以被打包下载到本地SQL Server服务器上:您还可以通过数据同步服务(Data Sync Service)备份数据库,以及进行动态数据同步. 数据层应用程序 数据层应用程序(Data-Tier Application, DAC)封装了一个应用程序所用的所有数据库和实例对象,例如表.索引.视图.存储过程乃至用户等. 通过这个逻辑上的封装,开发者

在C#中运用SQLDMO备份和恢复SQL Server数据库

server|备份|恢复|数据|数据库     SQLDMO(SQL Distributed Management Objects,SQL分布式管理对象)封装了Microsoft SQL Server数据库中的对象.SQLDMO是Microsoft SQL Server中企业管理器所使用的应用程序接口,所以它可以执行很多功能,其中当然也包括对数据库的备份和恢复. SQLDMO由Microsoft SQL Server自带的SQLDMO.dll提供,由于SQLDMO.dll是一个COM对象(有关C

java中以windows集成方式连接SQL Server

今天有人问起,如何以windows集成方式连接SQL Server,这个以前真没试过. 于是,打开netBeans测试了一下,代码如下: /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package testsqlconn; import java.sql.*; import com.microsoft.sqlserver.jdbc.*; /** *

急求帮助!!visual studio 2012 C#开发过程中 gridView中的数据在数据库进行sql语句查询,结果重复出现

问题描述 里面的sqlclass1是:sqlclass1=newSqlClass("SELECTOyear,Omonth,Oday,sum([price])FROM[台球厅台位预订系统].[dbo].[Order1]whereresult='预约成功'groupbyOday,Oyear,Omonth"); 解决方案 解决方案二:运行结果:解决方案三:sql语句在数据库中执行时没有错误,也不会重复,但是在程序运行时就出现错误了,不知道为什么.解决方案四:有没有考虑过重复绑定,SqlDat

mssqlserver中BULK INSERT大批量导入数据 SQL SERVER

开门见山,直接以例子介绍:  代码如下 复制代码 CREATE TABLE [dbo].[course]( [id] [int] NULL, [name] [nvarchar](50) NULL, [CourseType] [nvarchar](50) NULL, [Course] [float] NULL ) 导入数据: 将下面的数据存储为文本文件或SQL文件 2,李刚,语文,89;3,李刚,数学,79;3,李刚,英语,69;4,李刚,化学,89   导入语句:  代码如下 复制代码 BULK

mvc4中User.IsInRole默认会访问SQL Server Express的问题

问题描述 下面的代码在MVC3中验证运行是正常的,但在MVC4中,代码运行到此处会自动访问本地数据库,并且显示:连接字符串使用应用程序App_Data目录中的数据库位置指定了一个本地SQLServerExpress实例.我实际的数据库连接并不是这个,而是另外一个远程数据库.if(Roles.Any(httpContext.User.IsInRole)){returntrue;} 解决方案 解决方案二:webconfig里配置的是远程数据库连接吗?解决方案三:是的,我查遍了整个解决方案也没有SQL

Asp.net中把Excel数据存储至SQL Server中

  操作图   ExcelWrapper         /// <summary>         /// 查询EXCEL电子表格添加到DATASET         /// </summary>         /// <param name="filenameurl">文件路径</param>         /// <param name="table">dataset中的表名(并不是要和数据库中的

Asp.net中把Excel数据存储至SQL Server中的具体实现方法_实用技巧

ExcelWrapper 复制代码 代码如下:         /// <summary>        /// 查询EXCEL电子表格添加到DATASET        /// </summary>        /// <param name="filenameurl">文件路径</param>        /// <param name="table">dataset中的表名(并不是要和数据库中的表