当前位置:编程学习 > C#/ASP.NET >>

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>

补充:asp.net教程,.Net开发 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,