当前位置:首页 > 实现datagrid多层表头及datagrid列动态统计功能

实现datagrid多层表头及datagrid列动态统计功能

点击次数:1394  更新日期:2010-12-30
\n

前台代码


\n

<%@ Page language=”c#” Codebehind=”InSum.aspx.cs” AutoEventWireup=”false” Inherits=”FLX.Portal.InSum” %>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN” >
<HTML>
<HEAD>
<title>InSum</title>
<meta name=”GENERATOR” Content=”Microsoft Visual Studio .NET 7.1″>
<meta name=”CODE_LANGUAGE” Content=”C#”>
<meta name=”vs_defaultClientScript” content=”JavaScript”>
<meta name=”vs_targetSchema” content=”http://schemas.microsoft.com/intellisense/ie5″>
</HEAD>
<body>
<form id=”Form1″ method=”post” runat=”server”>
<asp:datagrid id=”DataGrid1″ runat=”server” AutoGenerateColumns=”False” Width=”100%” BorderColor=”#CCCCCC”
BorderStyle=”None” BorderWidth=”1px” BackColor=”White” CellPadding=”3″ AllowPaging=”True”
Font-Size=”12px” ShowFooter=”True”>
<FooterStyle ForeColor=”#000066″ BackColor=”White”></FooterStyle>
<SelectedItemStyle Font-Bold=”True” ForeColor=”White” BackColor=”#669999″></SelectedItemStyle>
<ItemStyle ForeColor=”#000066″ BorderColor=”#D4D0C8″></ItemStyle>
<HeaderStyle ForeColor=”Black” BackColor=”#E1EEFE”></HeaderStyle>
<Columns>
<asp:BoundColumn DataField=”id” HeaderText=”id”></asp:BoundColumn>
<asp:BoundColumn DataField=”count1″ HeaderText=”count1″></asp:BoundColumn>
<asp:BoundColumn DataField=”count2″ HeaderText=”count2″></asp:BoundColumn>
<asp:TemplateColumn HeaderText=”count3″>
<ItemTemplate>
<asp:Label id=lblQuantity runat=”server” Text=’<%# DataBinder.Eval(Container, “DataItem.count3″) %>’>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign=”Left” ForeColor=”#000066″ BackColor=”White” Mode=”NumericPages”></PagerStyle>
</asp:datagrid>
</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;


\n

namespace FLX.Portal
{
/**//// <summary>
/// InSum 的摘要说明。
/// </summary>
public class InSum : PortalPagePersonal
{
public int intSum1=0;
public int intSum2=0;
public int intSum3=0;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!this.IsPostBack)
{
this.DataGrid1.DataSource =this.GetDataBind();
this.DataGrid1 .DataBind();
}
}


\n

Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/**//// <summary>
/// 设计器支持所需的方法 – 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemCreated);
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);


\n

}
#endregion


\n

private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemIndex >= 0)
{
//取指定列的数据总和
intSum1 += int.Parse(e.Item.Cells[1].Text.ToString());
intSum2 += int.Parse(e.Item.Cells[2].Text.ToString());
Label lblQuantity = (Label)e.Item.Cells[3].FindControl(“lblQuantity”); 
intSum3 += int.Parse(lblQuantity.Text.ToString());
}
else if(e.Item.ItemType == ListItemType.Footer)
{
e.Item.Cells[0].Text = “<font color=’red’>总计:</font>”;   
e.Item.Cells[1].Text = “<font color=’red’>”+intSum1.ToString()+”</font>”; 
e.Item.Cells[2].Text = “<font color=’red’>”+intSum2.ToString()+”</font>”; 
e.Item.Cells[3].Text = “<font color=’red’>”+intSum3.ToString()+”</font>”; 
}
}
private DataTable GetDataBind()
{
string sql=”select * from Count”;
DataTable dt=new DataTable();
dt=this.DataAccessFacade.ExecuteDataTable(sql);
return dt;
}


\n

private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Header)
{
TableCellCollection tcl=e.Item.Cells;
tcl.Clear();
tcl.Add(new TableHeaderCell());
tcl[0].ColumnSpan =4;
tcl[0].Text=”第一季度个人消费情况表</th></tr><tr><td rowspan =\\”2\\”>姓名</td><td colspan=\\”3\\” align=\\”center\\”>帐目统计</td></tr><tr><td>项目一</td><td>项目二</td><td>项目三</td>”;
}
}
}
}
展示效果


\n


\n

来源:http://www.cnblogs.com/dreamof

\n