<%@ Page language="c#" Codebehind="ComputeBySNO.aspx.cs" AutoEventWireup="false" Inherits="DataGridInDataGrid.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>WebForm1</title>
  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
  <meta content="C#" name="CODE_LANGUAGE">
  <meta content="JavaScript" name="vs_defaultClientScript">
  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
 </HEAD>
 <body>
  <h2>按学号统计</h2>
  <hr align="left" width="80%" noShade SIZE="4">
  <br>
  <form >
            <HR>
           </TD>
          </TR>
         </TABLE>
        </ItemTemplate>
       </asp:TemplateColumn>
      </Columns>
     </asp:DataGrid></div>
   </FONT>
  </form>
 </body>
</HTML>
--------------------------------------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

namespace DataGridInDataGrid
{
 /// <summary>
 /// WebForm1 的摘要说明。
 /// </summary>
 public class WebForm1 : System.Web.UI.Page
 {
  private int SumScore=0;
  private int MaxScore=0;
  private int MinScore=100;//每门课的满分为100

  protected System.Web.UI.WebControls.DataGrid dgMaster;
  protected System.Web.UI.WebControls.DataGrid DataGrid2;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   if(!Page.IsPostBack)
   {
    this.BindData();
   }
  }

  private void BindData()
  {
   SqlConnection MyCn=new SqlConnection();
   MyCn.ConnectionString=@"Server=BOBO\PLAYBOY;Database=StudyStatus;Uid=sa;Pwd=840616";
  
   string str_sql = @"select * from students;select SNO,Course.CourseName,Score.Grade,Course.Teacher from Score,Course where Score.CourseNum=Course.CourseNum";
   SqlDataAdapter Myda=new SqlDataAdapter(str_sql,MyCn);

   DataSet ds=new DataSet();
   Myda.Fill(ds);
   ds.Tables[0].TableName="students";
   ds.Tables[1].TableName="Score";

   DataColumn parent=ds.Tables["students"].Columns["SNO"];
   DataColumn child=ds.Tables["Score"].Columns["SNO"];

   DataRelation tableRelation=new DataRelation("tableRelation",parent,child,false);
   ds.Relations.Add(tableRelation);

   
   this.dgMaster.DataSource=ds.Tables["students"].DefaultView;
   this.dgMaster.DataBind();
  }

  /// <summary>
  /// 模板列中的 DataGrid控件 的ItemDataBound事件
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  public void dgDetails_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
  {
   DataGrid dg=(DataGrid)(e.Item.Parent.Parent);
  
   if(e.Item.ItemIndex>=0)
   {
    Label lbl_score=(Label)e.Item.Cells[1].FindControl("lbl_score");
    int CurScore=int.Parse(lbl_score.Text);
    
    this.SumScore+=CurScore;
    if(this.MaxScore<CurScore)
    {
     this.MaxScore=CurScore;
    }
    if(this.MinScore>CurScore)
    {
     this.MinScore=CurScore;
    }
   }

   if(e.Item.ItemType==ListItemType.Footer)
   {
    if(dg.Items.Count!=0)
    {
     e.Item.Cells[0].Text="成绩统计:[总分]"+this.SumScore.ToString()+"    [平均分]"+String.Format("{0:N2}",(float)SumScore/(float)dg.Items.Count);
     e.Item.Cells[1].Text="[最高分]"+this.MaxScore.ToString();
     e.Item.Cells[2].Text="[最低分]"+this.MinScore.ToString();

     //绑定完后,数据恢复
     this.SumScore=this.MaxScore=0;
     this.MinScore=100;
    }
   }
  }
  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion
 }
}

<%@ Page language="c#" Codebehind="ComputeBySNO.aspx.cs" AutoEventWireup="false" Inherits="DataGridInDataGrid.WebForm1" %>

相关文章:

  • 2021-11-27
  • 2022-02-12
  • 2021-11-14
  • 2022-12-23
  • 2021-08-13
  • 2022-12-23
  • 2022-01-30
  • 2021-12-24
猜你喜欢
  • 2021-08-14
  • 2021-06-05
  • 2022-02-03
  • 2022-12-23
  • 2022-02-20
  • 2021-06-30
相关资源
相似解决方案