数据库中表
ID 2010-01-01 2010-01-02 ……
1 1 0
2 0 1
3 1 0
我需要在datalist中显示这些内容
但表的字段不固定
有没办法让datalist自动生成?
还有
我需要统计 每列的总数值
如 2010-01-01 总数值为 2
2010-01-02 总值为1
需要怎么写才能完成需要作出这样效果
ID 2010-01-01 2010-01-02 ……
1 1 0
2 0 1
3 1 0
sum 2 1哪位高手能指点下
ID 2010-01-01 2010-01-02 ……
1 1 0
2 0 1
3 1 0
我需要在datalist中显示这些内容
但表的字段不固定
有没办法让datalist自动生成?
还有
我需要统计 每列的总数值
如 2010-01-01 总数值为 2
2010-01-02 总值为1
需要怎么写才能完成需要作出这样效果
ID 2010-01-01 2010-01-02 ……
1 1 0
2 0 1
3 1 0
sum 2 1哪位高手能指点下
动态添加列也可以在数据源dt中增加了列与数据,进行统计信息后再进行绑定!
前台:<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None"
BorderWidth="1px" CellPadding="3" CellSpacing="2" DataKeyNames="产品编号" DataSourceID="SqlDataSource1"
ShowFooter="True" Width="624px" OnRowCreated="GridView1_RowCreated" OnRowDataBound="GridView1_RowDataBound">
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<Columns>
<asp:BoundField DataField="产品编号" HeaderText="产品编号" InsertVisible="False" ReadOnly="True"
SortExpression="产品编号" />
<asp:BoundField DataField="单价" HeaderText="单价" SortExpression="单价" />
<asp:BoundField DataField="库存量" HeaderText="库存量" SortExpression="库存量" />
<asp:BoundField DataField="已订购量" HeaderText="已订购量" SortExpression="已订购量" />
<asp:TemplateField HeaderText="订货金额" SortExpression="订货金额">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("订货金额", "{0:c}") %>'></asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:Label ID="OrderTotalLabel" runat="server" Font-Underline="True" ForeColor="Red"></asp:Label>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("订货金额", "{0:c}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedRowStyle BackColor="#C0FFC0" Font-Bold="True" ForeColor="Black" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:chtNorthwind %>"
SelectCommand="SELECT 产品编号, 单价, 库存量, 已订购量, 单价 * 已订购量 AS 订货金额 FROM 产品资料"></asp:SqlDataSource>
<i>您当前正在查看的页码:<b><font color="#ff0000"><%=GridView1.PageIndex + 1%>
/
<%=GridView1.PageCount%>
</font></b></i>
后台:using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Drawing;public partial class CH3_DemoForm008 : System.Web.UI.Page
{
// 创建一个变量来存储订货金额加总。
private decimal orderTotal = 0.0m; protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
// 提取当前的资料列。
GridViewRow row = e.Row; // 如果正被创建的数据列是一个页尾,则更新数据行加总。
if (row.RowType == DataControlRowType.Footer)
{
// 取得页尾当中的标签控件 OrderTotalTotal 。
Label total = (Label)(e.Row.FindControl("OrderTotalLabel")); // 以货币格式来显示订货金额加总。
if (total != null)
{
total.Text = orderTotal.ToString("c");
}
}
} protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// 确认"库存量"字段的值。
//
// 我们透过一个 DataBinder.Eval() 调用从将被绑定至 GridView 数据列的
// 数据中取得"库存量"字段的值,传递给 DataBinder.Eval() 的第一个参
// 数是将被绑定至 GridView 数据列的数据(也就是 e.Row.DataItem),
// 传递给 DataBinder.Eval() 的第二个参数则是字段名称。
decimal stock =
Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "库存量")); if (stock <= 0)
{
// 如果库存量小于或等于 0,则将该资料列的背景色设定成红色。
e.Row.BackColor = Color.Red;
} decimal totalMoney =
Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "订货金额")); if (totalMoney > 0)
{
// 如果订货金额大于 0,则将该资料列的背景色设定成黄色。
e.Row.BackColor = Color.Yellow;
} // 累加订货金额并设定给变量 orderTotal。
orderTotal += totalMoney;
}
}
}
AutoGenerateColumns="False" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None"
BorderWidth="1px" CellPadding="3" CellSpacing="2" DataKeyNames="产品编号" DataSourceID="SqlDataSource1"
ShowFooter="True" Width="624px" OnRowCreated="GridView1_RowCreated" OnRowDataBound="GridView1_RowDataBound">
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<Columns>
<asp:BoundField DataField="产品编号" HeaderText="产品编号" InsertVisible="False" ReadOnly="True"
SortExpression="产品编号" />
<asp:BoundField DataField="单价" HeaderText="单价" SortExpression="单价" />
<asp:BoundField DataField="库存量" HeaderText="库存量" SortExpression="库存量" />
<asp:BoundField DataField="已订购量" HeaderText="已订购量" SortExpression="已订购量" />
<asp:TemplateField HeaderText="订货金额" SortExpression="订货金额">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("订货金额", "{0:c}") %>'></asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:Label ID="OrderTotalLabel" runat="server" Font-Underline="True" ForeColor="Red"></asp:Label>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("订货金额", "{0:c}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedRowStyle BackColor="#C0FFC0" Font-Bold="True" ForeColor="Black" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:chtNorthwind %>"
SelectCommand="SELECT 产品编号, 单价, 库存量, 已订购量, 单价 * 已订购量 AS 订货金额 FROM 产品资料"></asp:SqlDataSource>
<i>您当前正在查看的页码:<b><font color="#ff0000"><%=GridView1.PageIndex + 1%>
/
<%=GridView1.PageCount%>
</font></b></i>
但我现在需要动态生成 能不能写的?
数据库中表
ID 2010-01-01 2010-01-02 …… 2010-01-30
1 1 0 1
2 0 1 0
3 1 0 0
要实现
ID 2010-01-01 2010-01-02 …… 2010-01-30
1 1 0 1
2 0 1 0
3 1 0 0
sum 2 1 1