扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
第一次碰到这么复杂的问题
对表a进行自定义统计,行和列都是可以无限选的
行:字段1(一级) 字段2(二级)....
列:字段3(一级) 字段4(二级)....
行头以字段1为例
select distinct 字段1 from a
值1
值2
图中x的等效形式为select x=sum(字段5) from a where 字段1=字段1.值1 and 字段3=字段3.值1 and 字段2=字段2.值2 and 字段4=字段4.值2
求数据查询和表生成的最优解决方案
参考
<asp:GridView ID="GridView1" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns=false runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%#Container.DataItem.ToString()%>
<!--第二层GridView开始-->
<asp:GridView ID="GridView2" OnRowDataBound="GridView2_RowDataBound" AutoGenerateColumns=false runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%#Container.DataItem.ToString()%>
<!--第三层GridView开始-->
<asp:GridView ID="GridView3" AutoGenerateColumns=false runat=server>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%#Container.DataItem.ToString()%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<!--第三层GridView 结束-->
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<!--第二层GridView结束-->
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<!--第一层GridView结束-->
++++++++++.cs代码++++++++++++++++
RowDataBound与1.x中的ItemDataBound也有一腿!
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.GridView1.DataSource = Al("GridView1Item:");
this.GridView1.DataBind();
}
//DataSource
protected ArrayList Al(string GetStr)
{
ArrayList al = new ArrayList();
for(int i=0;i<2;i++)
{
al.Add(GetStr+i.ToString());
}
return al;
}
//GridView1_RowDataBound
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView gv = (GridView)e.Row.FindControl("GridView2");
gv.DataSource = Al("GridView2Item:");
gv.DataBind();
}
}
//GridView2_RowDataBound
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView gv = (GridView)e.Row.FindControl("GridView3");
gv.DataSource = Al("GridView3Item:");
gv.DataBind();
}
}
}
--测试用表
CREATE TABLE [dbo].[Table_1](
[id] [int] IDENTITY(1,1) NOT NULL,
[f1] [nvarchar](50) NULL,
[f2] [nvarchar](50) NULL,
[f3] [nvarchar](50) NULL,
[f4] [nvarchar](50) NULL,
[f5] [int] NULL,
)
--测试数据
insert Table_1(f1,f2,f3,f4,f5) values('男','大','黑','上',1)
insert Table_1(f1,f2,f3,f4,f5) values('男','大','黑','下',2)
insert Table_1(f1,f2,f3,f4,f5) values('男','大','白','上',3)
insert Table_1(f1,f2,f3,f4,f5) values('男','大','白','下',4)
insert Table_1(f1,f2,f3,f4,f5) values('男','小','黑','上',5)
insert Table_1(f1,f2,f3,f4,f5) values('男','小','黑','下',6)
insert Table_1(f1,f2,f3,f4,f5) values('男','小','白','上',7)
insert Table_1(f1,f2,f3,f4,f5) values('男','小','白','下',8)
insert Table_1(f1,f2,f3,f4,f5) values('女','大','黑','上',9)
insert Table_1(f1,f2,f3,f4,f5) values('女','大','黑','下',10)
insert Table_1(f1,f2,f3,f4,f5) values('女','大','白','上',11)
insert Table_1(f1,f2,f3,f4,f5) values('女','大','白','下',12)
insert Table_1(f1,f2,f3,f4,f5) values('女','小','黑','上',13)
insert Table_1(f1,f2,f3,f4,f5) values('女','小','黑','下',14)
insert Table_1(f1,f2,f3,f4,f5) values('女','小','白','上',15)
insert Table_1(f1,f2,f3,f4,f5) values('女','小','白','下',16)
行:f1{男,女} f2{大,小}
列:f3{黑,白} f4{上,下}
用repeater就能做,他是用户自定义界面控件:参考一下我做过的,如下
界面代码:你可以复制过去看一下效果
<div>
<table border="0" cellpadding="0" cellspacing="0" style="width: 884px; height: 288px">
<tr>
<td align="center" style="width: 100%; height: 58px">
<asp:HyperLink ID="HyperLink1" runat="server" Font-Size="14pt" NavigateUrl="~/ShopInfoManage.aspx"
Width="80px">返 回 </asp:HyperLink> </td>
</tr>
<tr>
<td style="width: 100%">
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<HeaderTemplate>
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<td align="center" rowspan="3" style="width: 100px">
<span>项目 </span> </td>
<td align="center" colspan="2">
合计 </td>
<td align="center" colspan="2">
铁 </td>
<td align="center" colspan="4">
钢筋 </td>
</tr>
<tr>
<td align="center" style="width: 100px" rowspan="2">
吨 </td>
<td align="center" style="width: 100px" rowspan="2">
% </td>
<td align="center" style="width: 100px" rowspan="2">
吨 </td>
<td align="center" style="width: 100px" rowspan="2">
% </td>
<td align="center" style="width: 100px" rowspan="2">
吨 </td>
<td align="center" style="width: 100px" rowspan="2">
% </td>
<td align="center" colspan="2">
其中 </td>
</tr>
<tr>
<td align="center" style="width: 100px">
一等 </td>
<td align="center" style="width: 100px">
二等 </td>
</tr>
</HeaderTemplate>
<FooterTemplate>
<tr>
<td style="width: 100px">合计 </td>
<td align="center" style="width: 100px">
<asp:Label ID="Label10" runat="server" Text=''> </asp:Label> </td>
<td align="center" style="width: 100px">
<asp:Label ID="Label11" runat="server" Text=''> </asp:Label> </td>
<td align="center" style="width: 100px">
<asp:Label ID="Label12" runat="server" Text=''> </asp:Label> </td>
<td align="center" style="width: 100px">
<asp:Label ID="Label13" runat="server" Text=''> </asp:Label> </td>
<td align="center" style="width: 100px">
<asp:Label ID="Label14" runat="server" Text=''> </asp:Label> </td>
<td align="center" style="width: 100px">
<asp:Label ID="Label15" runat="server" Text=''> </asp:Label> </td>
<td align="center" style="width: 100px">
<asp:Label ID="Label16" runat="server" Text=''> </asp:Label> </td>
<td align="center" style="width: 100px">
<asp:Label ID="Label17" runat="server" Text=''> </asp:Label> </td>
</tr>
</table>
</FooterTemplate>
<ItemTemplate>
<tr>
<td style="width: 100px">
<asp:Label ID="Label1" runat="server" Text=' <% # Bind("ShopID") %>'> </asp:Label>
</td>
<td align="center" style="width: 100px">
<asp:Label ID="Label2" runat="server" Text=' <% # Bind("ShopCount") %>'> </asp:Label>
</td>
<td align="center" style="width: 100px">
<asp:Label ID="Label3" runat="server" Text=' <% # Bind("CountCenti") %>'> </asp:Label>
</td>
<td align="center" style="width: 100px">
<asp:Label ID="Label4" runat="server" Text=' <% # Bind("TT") %>'> </asp:Label>
</td>
<td align="center" style="width: 100px">
<asp:Label ID="Label5" runat="server" Text=' <% # Bind("TTCenti") %>'> </asp:Label>
</td>
<td align="center" style="width: 100px">
<asp:Label ID="Label6" runat="server" Text=' <% # Bind("GGCount") %>'> </asp:Label>
</td>
<td align="center" style="width: 100px">
<asp:Label ID="Label7" runat="server" Text=' <% # Bind("GGCenti") %>'> </asp:Label>
</td>
<td align="center" style="width: 100px">
<asp:Label ID="Label8" runat="server" Text=' <% # Bind("GG1D") %>'> </asp:Label>
</td>
<td align="center" style="width: 100px">
<asp:Label ID="Label9" runat="server" Text=' <% # Bind("GG2D") %>'> </asp:Label>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</td>
</tr>
</table>
</div>
用微软的报表控件,加个矩阵可以实现这个效果
只不过这里需要通过编程方式生成一个客户端报表定义 (.rdlc) 文件,xml格式
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者