1.datagrid多层表头功能的实现
实际上Asp.Net下的DataGrid是一个HtmlTable,只不过在HtmlTable的基础上添加了很多属性、方法,纳入ViewState机制,来生成、控制它;有了这一点认识,事情就很好办了,我们可以在属性生成器中定义列的表头,它实际上只不过是在列之间插入了“</td><td>”的html标记,如此而已。
我们可以在DataGrid中的ItemCreated事件中处理,代码如下:
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].RowSpan=2;
tcl[0].Text="项目";
tcl.Add(new TableHeaderCell());
tcl[1].ColumnSpan =4;
tcl[1].Text="第一季度个人消费情况表</th></tr><tr><td rowspan =\"2\">姓名</td><td colspan=\"3\" align=\"center\">帐目统计</td></tr><tr><td>项目一</td><td>项目二</td><td>项目三</td>";
}
}
{
if(e.Item.ItemType==ListItemType.Header)
{
TableCellCollection tcl=e.Item.Cells;
tcl.Clear();
tcl.Add(new TableHeaderCell());
tcl[0].RowSpan=2;
tcl[0].Text="项目";
tcl.Add(new TableHeaderCell());
tcl[1].ColumnSpan =4;
tcl[1].Text="第一季度个人消费情况表</th></tr><tr><td rowspan =\"2\">姓名</td><td colspan=\"3\" align=\"center\">帐目统计</td></tr><tr><td>项目一</td><td>项目二</td><td>项目三</td>";
}
}
2.datagrid列动态统计功能的实现
首先需要允许DataGrid的页脚显示,用于显示统计列值;
具体的实现可以在DataGrid中的ItemCreated事件中处理,代码如下: (intSum1,intSum2,intSum3是全局变量)
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>";
}
}
{
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>";
}
}
3.综合实例
为了帮助大家理解上面功能的实现,我把一个具体实例的代码贴出来以供大家参考!
前台代码
<%@ 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>
后台代码<!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;
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();
}
}
Web 窗体设计器生成的代码
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;
}
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>";
}
}
}
}
效果展示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;
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();
}
}
Web 窗体设计器生成的代码
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;
}
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>";
}
}
}
}