ASPxGridView数据汇总收藏
ASPxGridView数据汇总收藏
数据汇总一般都是列汇总,也有需要横向的,ASPxGridView在这方面提供了支持实现起来很简单。这里只是把数据库改成常用的Northwind以便调试。
1.列汇总
只需要设置TotalSummary属性就能实现一般的汇总,后台不需要代码。下面是前台代码
<dxwgv:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" KeyFieldName="OrderID" >
<TotalSummary>
<dxwgv:ASPxSummaryItem FieldName="Subtotal" SummaryType="Sum" />
<dxwgv:ASPxSummaryItem FieldName="OrderID" SummaryType="Count" />
<dxwgv:ASPxSummaryItem FieldName="ShippedDate" SummaryType="Max" />
</TotalSummary>
<Settings ShowFooter="True" />
<Styles>
<Footer BackColor="#6699FF">
</Footer>
</Styles>
<Columns>
<dxwgv:GridViewDataDateColumn FieldName="ShippedDate" VisibleIndex="1">
</dxwgv:GridViewDataDateColumn>
<dxwgv:GridViewDataTextColumn FieldName="OrderID" ReadOnly="True" VisibleIndex="0"><EditFormSettings Visible="False" />
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataTextColumn FieldName="Subtotal" VisibleIndex="2">
</dxwgv:GridViewDataTextColumn>
</Columns>
</dxwgv:ASPxGridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT * FROM [Summary of Sales by Year]">
</asp:SqlDataSource>链接字符串:<connectionStrings>
<add name="NorthwindConnectionString" connectionString="Data Source=.;Initial Catalog=Northwind;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
2.横向汇总
横向汇总在ASPxGridView中先添加一个显示你要的数据的列然后在后台给列中的元素赋值就可以了。赋值的代码写在 grid_CustomUnboundColumnData事件里。前台代码:
<dxwgv:ASPxGridView ID="grid" ClientInstanceName="grid" runat="server" DataSourceID="SqlDataSource1" KeyFieldName="OrderID"
OnCustomUnboundColumnData="grid_CustomUnboundColumnData" Width="100%" AutoGenerateColumns="False">
<Columns>
<dxwgv:GridViewDataDateColumn FieldName="ShippedDate" VisibleIndex="1">
</dxwgv:GridViewDataDateColumn>
<dxwgv:GridViewDataTextColumn FieldName="OrderID" ReadOnly="True" VisibleIndex="0"><EditFormSettings Visible="False" />
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataTextColumn FieldName="Subtotal" VisibleIndex="2">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataTextColumn FieldName="Total" VisibleIndex="3" UnboundType="Decimal">
<FooterCellStyle ForeColor="Brown"> </FooterCellStyle>
</dxwgv:GridViewDataTextColumn>
</Columns>
<Settings ShowGroupPanel="True" ShowFooter="True" />
<TotalSummary>
<dxwgv:ASPxSummaryItem FieldName="OrderID" SummaryType="Count"/>
<dxwgv:ASPxSummaryItem FieldName="Subtotal" SummaryType="Sum" />
<dxwgv:ASPxSummaryItem FieldName="Subtotal" SummaryType="Min" />
<dxwgv:ASPxSummaryItem FieldName="Subtotal" SummaryType="Average" />
<dxwgv:ASPxSummaryItem FieldName="Subtotal" SummaryType="Max" />
</TotalSummary>
</dxwgv:ASPxGridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT * FROM [Summary of Sales by Year]">
</asp:SqlDataSource>
后台: // 横向汇总事用到这个方法,可以使一行中的某几列经过计算得到一列。
protected void grid_CustomUnboundColumnData(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDataEventArgs e)
{
if (e.Column.FieldName == "Total")
{
decimal price = (decimal)e.GetListSourceFieldValue("Subtotal");
e.Value = price * 2;
}
}
下面是Devexpress的官方文献跟大概的翻译:
Online Demo : http://demos.devexpress.com/ASPxGridViewDemos/Summary/Total.aspx
原文:
Total summaries are aggregate function values calculated over all the rows within the ASPxGridView and displayed within the Footer.
The Footer is displayed if the Settings.ShowFooter property is set to true.
The ASPxGridView stores its total summary items within the TotalSummary collection. Individual items are represented by ASPxSummaryItem objects.
翻译:
汇总摘要计算aspxgridview的所有行求得平均或总和并显示在页脚。
当settings.showfooter属性设置为True,才显示页脚。
aspxgridview的汇总条目是放在totalsummary属性里。个别条目被ASPxSummaryItem对象描述。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jjjjj102310253/archive/2008/05/28/2489231.aspx