DataGrid 完全攻略之四 (实现统计)


前台代码:html
ExpandedBlockStart.gif <% @ Page language="c#" Codebehind="UserCount.aspx.cs" AutoEventWireup="false" Inherits="MsDataGrid.UserCount"  %>
None.gif
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  >
None.gif
< HTML >
None.gif    
< HEAD >
None.gif        
< title > DataGrid使用举例 </ title >
None.gif        
< meta  name ="GENERATOR"  Content ="Microsoft Visual Studio 7.0" >
None.gif        
< meta  name ="CODE_LANGUAGE"  Content ="C#" >
None.gif        
< meta  name ="vs_defaultClientScript"  content ="JavaScript" >
None.gif        
< meta  name ="vs_targetSchema"  content ="http://schemas.microsoft.com/intellisense/ie5" >
None.gif    
</ HEAD >
None.gif    
< body  MS_POSITIONING ="GridLayout" >
None.gif        
< form  id ="Form1"  method ="post"  runat ="server" >
None.gif            
< FONT  face ="宋体" >
None.gif                
< 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" >
None.gif                    
< SelectedItemStyle  ForeColor ="GhostWhite"  BackColor ="DarkSlateBlue" ></ SelectedItemStyle >
None.gif                    
< AlternatingItemStyle  BackColor ="PaleGoldenrod" ></ AlternatingItemStyle >
None.gif                    
< HeaderStyle  Font-Bold ="True"  BackColor ="Tan" ></ HeaderStyle >
None.gif                    
< FooterStyle  BackColor ="Tan" ></ FooterStyle >
None.gif                    
< Columns >
None.gif                        
< asp:BoundColumn  DataField ="StudentID"  ReadOnly ="True"  HeaderText ="学生ID" ></ asp:BoundColumn >
None.gif                        
< asp:BoundColumn  DataField ="StudentName"  HeaderText ="学生姓名" ></ asp:BoundColumn >
None.gif                        
< asp:BoundColumn  DataField ="StudentPass"  HeaderText ="密码" ></ asp:BoundColumn >
None.gif                        
< asp:BoundColumn  DataField ="Sex"  HeaderText ="性别" ></ asp:BoundColumn >
None.gif                        
< asp:BoundColumn  DataField ="Birthday"  HeaderText ="生日"  DataFormatString ="{0:yyyy-M-d}" ></ asp:BoundColumn >
None.gif                        
< asp:BoundColumn  DataField ="Email"  HeaderText ="邮件地址" ></ asp:BoundColumn >
None.gif                        
< asp:BoundColumn  DataField ="Score"  HeaderText ="分数" ></ asp:BoundColumn >
None.gif                    
</ Columns >
None.gif                    
< PagerStyle  HorizontalAlign ="Center"  ForeColor ="DarkSlateBlue"  BackColor ="PaleGoldenrod" ></ PagerStyle >
None.gif                
</ asp:DataGrid ></ FONT >
None.gif        
</ form >
None.gif    
</ body >
None.gif
</ HTML >
None.gif

后台代码:cs
None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  System.Data.SqlClient;
None.gif
namespace  MsDataGrid
ExpandedBlockStart.gif
{
ExpandedSubBlockStart.gif    
/// <summary>
InBlock.gif    
/// WebForm1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class UserCount : System.Web.UI.Page
ExpandedSubBlockStart.gif    
{
InBlock.gif        
protected System.Web.UI.WebControls.DataGrid dgShow;
InBlock.gif    
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
// 在此处放置用户代码以初始化页面
InBlock.gif
            if(!IsPostBack)
InBlock.gif                BindData();
InBlock.gif            
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void BindData()
ExpandedSubBlockStart.gif        
{
InBlock.gif            
string strCon = System.Configuration.ConfigurationSettings.AppSettings["DSN"];
InBlock.gif            SqlConnection con 
= new SqlConnection(strCon);
InBlock.gif            SqlDataAdapter da 
= new SqlDataAdapter("Select * from tbStudentinfo",con);
InBlock.gif            DataSet ds 
= new DataSet();
InBlock.gif            da.Fill(ds,
"studentinfo");
InBlock.gif            dgShow.DataSource 
= ds.Tables["studentinfo"].DefaultView;
InBlock.gif            dgShow.DataBind();
InBlock.gif            
//以下作分数和的统计
InBlock.gif
            int count=0;
InBlock.gif            
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
ExpandedSubBlockStart.gif            
{
InBlock.gif                count 
+= int.Parse(ds.Tables[0].Rows[i]["Score"].ToString());
ExpandedSubBlockEnd.gif            }

InBlock.gif            
int nAv = count/ds.Tables[0].Rows.Count;
InBlock.gif            
foreach(DataGridItem dgi in dgShow.Controls[0].Controls)
ExpandedSubBlockStart.gif            
{
InBlock.gif                
if (dgi.ItemType == ListItemType.Footer)
InBlock.gif                    dgi.Cells[
6].Text = "平均:"+nAv.ToString();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
ExpandedSubBlockEnd.gif        }

ContractedSubBlock.gif        
Web Form Designer generated code
InBlock.gif
InBlock.gif        
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif



本文转自高海东博客园博客,原文链接http://www.cnblogs.com/ghd258/archive/2005/10/12/253193.html,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值