ASPxGridView数据汇总收藏

一开始不了解这个控件,在公司接触后发现特别好用。

ASPx­Grid­View数据汇总收藏
数据汇总一般都是列汇总,也有需要横向的,ASPxGridView在这方面提供了支持实现起来很简单。这里只是把数据库改成常用的Northwind以便调试。

1.列汇总

只需要设置TotalSummary属性就能实现一般的汇总,后台不需要代码。下面是前台代码

<dxwgv:ASPxGridView ID=“ASPxGridView1” runat=“server”  AutoGenerateColumns=“False” DataSourceID=“SqlDataSource1” KeyFieldName=“OrderID” >
<Total­Sum­ma­ry>
<dxwgv:ASPxSummaryItem FieldName=“Subtotal” SummaryType=“Sum”  />
<dxwgv:ASPxSummaryItem FieldName=“OrderID”   SummaryType=“Count” />
<dxwgv:ASPxSummaryItem FieldName=“ShippedDate” SummaryType=“Max” />
</TotalSummary>
<Set­tings ShowFooter=“True” />
<Styles>
<Foot­er 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”
Con­nec­tion­String=”<%$ ConnectionStrings:NorthwindConnectionString %>”
SelectCommand=“SELECT * FROM [Sum­ma­ry 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”>
<Footer­Cell­Style ForeColor=“Brown”> </FooterCellStyle>
</dxwgv:GridViewDataTextColumn>
</Columns>
<Set­tings ShowGroupPanel=“True” ShowFooter=“True” />
<Total­Sum­ma­ry>
<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”
Con­nec­tion­String=”<%$ ConnectionStrings:NorthwindConnectionString %>”
SelectCommand=“SELECT * FROM [Sum­ma­ry of Sales by Year]”>
</asp:SqlDataSource>
后台:     // 横向汇总事用到这个方法,可以使一行中的某几列经过计算得到一列。
pro­tect­ed void grid_CustomUnboundColumnData(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDataEventArgs e)
{
if (e.Column.FieldName == “Total”)
{
dec­i­mal price = (decimal)e.GetListSourceFieldValue(“Subtotal”);
e.Value = price * 2;
}
}

 

<asp:SqlDataSource ID=“SqlDataSource1” runat=“server”
Con­nec­tion­String=”<%$ ConnectionStrings:NorthwindConnectionString %>”
SelectCommand=“SELECT * FROM [Sum­ma­ry of Sales by Year]”>
</asp:SqlDataSource>
后台:     // 横向汇总事用到这个方法,可以使一行中的某几列经过计算得到一列。
pro­tect­ed void grid_CustomUnboundColumnData(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDataEventArgs e)
{
if (e.Column.FieldName == “Total”)
{
dec­i­mal price = (decimal)e.GetListSourceFieldValue(“Subtotal”);
e.Value = price * 2;
}
}

 

项目实例截图:

汇总摘要计算aspx­grid­view的所有行求得平均或总和并显示在页脚。

当settings.showfooter属性设置为True,才显示页脚。

aspxgridview的汇总条目是放在totalsummary属性里。个别条目被ASPxSummaryItem对象描述。

 

 

为您推荐

发表评论

电子邮件地址不会被公开。 必填项已用*标注