Default27.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default27.aspx.cs" Inherits="Default27" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource1"
OnRowCreated="GridView1_RowCreated">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False"
ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="name" HeaderText="姓名" SortExpression="name" />
<asp:BoundField DataField="student_id" HeaderText="學號"
SortExpression="student_id" />
<asp:BoundField DataField="city" HeaderText="居住地" SortExpression="city" />
<asp:TemplateField HeaderText="國語" SortExpression="chinese">
<ItemTemplate>
<asp:Label ID="lbl_chinese" runat="server" Text='<%# myNo1 %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="數學" SortExpression="math">
<ItemTemplate>
<asp:Label ID="lbl_math" runat="server" Text='<%# myNo2 %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="總分">
<ItemTemplate>
<asp:Label ID="lbl_total" runat="server" Text='<%#myTotal %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT * FROM [student_test]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
Default27.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
public partial class Default27 : System.Web.UI.Page
{
public int myNo1, myNo2, myTotal;
private double averageChinese, averageMath;
public int i = 0;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter("select * from student_test", conn);
DataSet ds = new DataSet();
da.Fill(ds, "test");
if (e.Row.RowType == DataControlRowType.DataRow)
{
myNo1 = Convert.ToInt32(ds.Tables["test"].Rows[i]["chinese"]);
myNo2 = Convert.ToInt32(ds.Tables["test"].Rows[i]["math"]);
averageChinese += myNo1;
averageMath += myNo2;
myTotal = myNo1 + myNo2;
i++;
}
if (e.Row.RowType != DataControlRowType.DataRow && e.Row.RowType != DataControlRowType.Header)
{
GridViewRow vr = new GridViewRow((i+1), -1, DataControlRowType.DataRow, DataControlRowState.Normal);
for (int j = 0; j < 7; j++)
{
TableCell cell = new TableCell();
cell.BackColor = System.Drawing.Color.Yellow;
Label mylabel = new Label();
if (j == 0)
{
mylabel.Text = "平均";
}
if (j == 4)
{
mylabel.Text = (Math.Round( (averageChinese / i),2)).ToString();
}
if (j == 5)
{
mylabel.Text = (Math.Round((averageMath / i), 2)).ToString();
}
cell.Controls.Add(mylabel);
vr.Cells.Add(cell);
}
GridView1.Controls[0].Controls.AddAt((i+1), vr);
}
}
}