
 <%
<% @ Page language="c#" Codebehind="UserCount.aspx.cs" AutoEventWireup="false" Inherits="MsDataGrid.UserCount" %>
@ Page language="c#" Codebehind="UserCount.aspx.cs" AutoEventWireup="false" Inherits="MsDataGrid.UserCount" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML>
<HTML> <HEAD>
    <HEAD> <title>DataGrid使用举例</title>
        <title>DataGrid使用举例</title> <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
        <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0"> <meta name="CODE_LANGUAGE" Content="C#">
        <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript">
        <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD>
    </HEAD> <body MS_POSITIONING="GridLayout">
    <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server">
        <form id="Form1" method="post" runat="server"> <FONT face="宋体">
            <FONT face="宋体"> <asp:DataGrid id="dgShow" style="Z-INDEX: 101; LEFT: 31px; POSITION: absolute; TOP: 93px" runat="server" Width="842px" Height="172px" BorderColor="Tan" BorderWidth="1px" BackColor="LightGoldenrodYellow" CellPadding="2" GridLines="None" ForeColor="Black" PageSize="1" AutoGenerateColumns="False" ShowFooter="True">
                <asp:DataGrid id="dgShow" style="Z-INDEX: 101; LEFT: 31px; POSITION: absolute; TOP: 93px" runat="server" Width="842px" Height="172px" BorderColor="Tan" BorderWidth="1px" BackColor="LightGoldenrodYellow" CellPadding="2" GridLines="None" ForeColor="Black" PageSize="1" AutoGenerateColumns="False" ShowFooter="True"> <SelectedItemStyle ForeColor="GhostWhite" BackColor="DarkSlateBlue"></SelectedItemStyle>
                    <SelectedItemStyle ForeColor="GhostWhite" BackColor="DarkSlateBlue"></SelectedItemStyle> <AlternatingItemStyle BackColor="PaleGoldenrod"></AlternatingItemStyle>
                    <AlternatingItemStyle BackColor="PaleGoldenrod"></AlternatingItemStyle> <HeaderStyle Font-Bold="True" BackColor="Tan"></HeaderStyle>
                    <HeaderStyle Font-Bold="True" BackColor="Tan"></HeaderStyle> <FooterStyle BackColor="Tan"></FooterStyle>
                    <FooterStyle BackColor="Tan"></FooterStyle> <Columns>
                    <Columns> <asp:BoundColumn DataField="StudentID" ReadOnly="True" HeaderText="学生ID"></asp:BoundColumn>
                        <asp:BoundColumn DataField="StudentID" ReadOnly="True" HeaderText="学生ID"></asp:BoundColumn> <asp:BoundColumn DataField="StudentName" HeaderText="学生姓名"></asp:BoundColumn>
                        <asp:BoundColumn DataField="StudentName" HeaderText="学生姓名"></asp:BoundColumn> <asp:BoundColumn DataField="StudentPass" HeaderText="密码"></asp:BoundColumn>
                        <asp:BoundColumn DataField="StudentPass" HeaderText="密码"></asp:BoundColumn> <asp:BoundColumn DataField="Sex" HeaderText="性别"></asp:BoundColumn>
                        <asp:BoundColumn DataField="Sex" HeaderText="性别"></asp:BoundColumn> <asp:BoundColumn DataField="Birthday" HeaderText="生日" DataFormatString="{0:yyyy-M-d}"></asp:BoundColumn>
                        <asp:BoundColumn DataField="Birthday" HeaderText="生日" DataFormatString="{0:yyyy-M-d}"></asp:BoundColumn> <asp:BoundColumn DataField="Email" HeaderText="邮件地址"></asp:BoundColumn>
                        <asp:BoundColumn DataField="Email" HeaderText="邮件地址"></asp:BoundColumn> <asp:BoundColumn DataField="Score" HeaderText="分数"></asp:BoundColumn>
                        <asp:BoundColumn DataField="Score" HeaderText="分数"></asp:BoundColumn> </Columns>
                    </Columns> <PagerStyle HorizontalAlign="Center" ForeColor="DarkSlateBlue" BackColor="PaleGoldenrod"></PagerStyle>
                    <PagerStyle HorizontalAlign="Center" ForeColor="DarkSlateBlue" BackColor="PaleGoldenrod"></PagerStyle> </asp:DataGrid></FONT>
                </asp:DataGrid></FONT> </form>
        </form> </body>
    </body> </HTML>
</HTML>
后台代码:cs
 using System;
using System; using System.Collections;
using System.Collections; using System.ComponentModel;
using System.ComponentModel; using System.Data;
using System.Data; using System.Drawing;
using System.Drawing; using System.Web;
using System.Web; using System.Web.SessionState;
using System.Web.SessionState; using System.Web.UI;
using System.Web.UI; using System.Web.UI.WebControls;
using System.Web.UI.WebControls; using System.Web.UI.HtmlControls;
using System.Web.UI.HtmlControls; using System.Data.SqlClient;
using System.Data.SqlClient; namespace MsDataGrid
namespace MsDataGrid

 {
{
 /**//// <summary>
    /**//// <summary> /// WebForm1 的摘要说明。
    /// WebForm1 的摘要说明。 /// </summary>
    /// </summary> public class UserCount : System.Web.UI.Page
    public class UserCount : System.Web.UI.Page
 
     {
{ protected System.Web.UI.WebControls.DataGrid dgShow;
        protected System.Web.UI.WebControls.DataGrid dgShow; 
     private void Page_Load(object sender, System.EventArgs e)
        private void Page_Load(object sender, System.EventArgs e)
 
         {
{ // 在此处放置用户代码以初始化页面
            // 在此处放置用户代码以初始化页面 if(!IsPostBack)
            if(!IsPostBack) BindData();
                BindData(); 
             
             }
        } private void BindData()
        private void BindData()
 
         {
{ string strCon = System.Configuration.ConfigurationSettings.AppSettings["DSN"];
            string strCon = System.Configuration.ConfigurationSettings.AppSettings["DSN"]; SqlConnection con = new SqlConnection(strCon);
            SqlConnection con = new SqlConnection(strCon); SqlDataAdapter da = new SqlDataAdapter("Select * from tbStudentinfo",con);
            SqlDataAdapter da = new SqlDataAdapter("Select * from tbStudentinfo",con); DataSet ds = new DataSet();
            DataSet ds = new DataSet(); da.Fill(ds,"studentinfo");
            da.Fill(ds,"studentinfo"); dgShow.DataSource = ds.Tables["studentinfo"].DefaultView;
            dgShow.DataSource = ds.Tables["studentinfo"].DefaultView; dgShow.DataBind();
            dgShow.DataBind(); //以下作分数和的统计
            //以下作分数和的统计 int count=0;
            int count=0; for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
 
             {
{ count += int.Parse(ds.Tables[0].Rows[i]["Score"].ToString());
                count += int.Parse(ds.Tables[0].Rows[i]["Score"].ToString()); }
            } int nAv = count/ds.Tables[0].Rows.Count;
            int nAv = count/ds.Tables[0].Rows.Count; foreach(DataGridItem dgi in dgShow.Controls[0].Controls)
            foreach(DataGridItem dgi in dgShow.Controls[0].Controls)
 
             {
{ if (dgi.ItemType == ListItemType.Footer)
                if (dgi.ItemType == ListItemType.Footer) dgi.Cells[6].Text = "平均:"+nAv.ToString();
                    dgi.Cells[6].Text = "平均:"+nAv.ToString(); }
            } 
             }
        }
 Web Form Designer generated code#region Web Form Designer generated code
        Web Form Designer generated code#region Web Form Designer generated code override protected void OnInit(EventArgs e)
        override protected void OnInit(EventArgs e)
 
         {
{ //
            // // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
            // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。 //
            // InitializeComponent();
            InitializeComponent(); base.OnInit(e);
            base.OnInit(e); }
        } 
        
 /**//// <summary>
        /**//// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。
        /// 此方法的内容。 /// </summary>
        /// </summary> private void InitializeComponent()
        private void InitializeComponent()
 
         {
{     this.Load += new System.EventHandler(this.Page_Load);
            this.Load += new System.EventHandler(this.Page_Load);
 }
        } #endregion
        #endregion
 
        
 }
    } }
}