首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Devexpress Gridview中计算SubTotal

在Devexpress Gridview中计算SubTotal
EN

Stack Overflow用户
提问于 2011-03-20 19:41:18
回答 2查看 4.3K关注 0票数 0

我有一个简单的Devexpress Gridview

这里的代码;

代码语言:javascript
复制
<dx:ASPxGridView ID="grid" runat="server" DataSourceID="MasterDataSource" Width="100%"
        AutoGenerateColumns="False" KeyFieldName="CategoryID">
        <Columns>
            <dx:GridViewDataTextColumn FieldName="CategoryID" ReadOnly="True" VisibleIndex="0" />
            <dx:GridViewDataTextColumn FieldName="CategoryName" VisibleIndex="1" />
            <dx:GridViewDataTextColumn FieldName="Description" VisibleIndex="2" />
        </Columns>
        <SettingsDetail ShowDetailRow="True" ExportMode="Expanded" />
        <Templates>
            <DetailRow>
                <dx:ASPxGridView ID="detailGrid" runat="server" AutoGenerateColumns="False" DataSourceID="DetailDataSource"
                    KeyFieldName="ProductID" Width="100%" OnBeforePerformDataSelect="detailGrid_BeforePerformDataSelect">
                    <SettingsDetail IsDetailGrid="true" ExportIndex="0" />
                    <Columns>
                        <dx:GridViewDataTextColumn FieldName="ProductID" ReadOnly="True" VisibleIndex="0" />
                        <dx:GridViewDataTextColumn FieldName="ProductName" VisibleIndex="1" />
                        <dx:GridViewDataTextColumn FieldName="UnitPrice" VisibleIndex="2" />
                        <dx:GridViewDataTextColumn FieldName="QuantityPerUnit" VisibleIndex="3" />
                    </Columns>
                </dx:ASPxGridView>

有任何方法来计算Unit Price列的小计吗?

对于这个例子,我只想要Unit Price列的总数(18 + 19 + 4.5 + 14 + 18 + 263.5 + 18 + 46 + 14 + 14 + 15 = 430)。

我怎么能这么做?

最好的问候,

Soner

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-03-21 19:27:43

可以使用以下方法使用ASPxGridView来实现这一点:

1)用单独的方法计算总结。2)使用detail的DataBound和BeforeGetCallBackResult事件处理程序来设置标签文本。这是我的代码:

代码语言:javascript
复制
protected int CalcPageSummary(ASPxGridView gridView, string fieldName) {
    GridViewDataColumn column = gridView.Columns[fieldName] as GridViewDataColumn;
    if(column == null)
        return 0;
    int pageIndex = gridView.PageIndex;
    int startRowIndex = pageIndex * gridView.SettingsPager.PageSize;
    int finishRowIndex = startRowIndex + gridView.SettingsPager.PageSize;
    if(finishRowIndex > gridView.VisibleRowCount)
        finishRowIndex = gridView.VisibleRowCount;
    int result = 0;
    for(int i = startRowIndex; i < finishRowIndex; i++) 
        result += Convert.ToInt32(gridView.GetRowValues(i, fieldName));
    return result;
}

private void SetColumnSummary(ASPxGridView gridView, string fieldName) {
    ASPxLabel lbl = gridView.FindFooterCellTemplateControl(gridView.Columns[fieldName], "ASPxLabel1") as ASPxLabel;
    if(lbl != null)
        lbl.Text = CalcPageSummary(gridView, fieldName).ToString();
}
protected void ASPxGridView2_BeforeGetCallbackResult(object sender, EventArgs e) {
    SetColumnSummary((ASPxGridView)sender, "UnitPrice");
}
protected void ASPxGridView2_BeforePerformDataSelect(object sender, EventArgs e) {
    Session["CategoryID"] = (sender as ASPxGridView).GetMasterRowKeyValue();
}
protected void ASPxGridView2_DataBound(object sender, EventArgs e) {
    SetColumnSummary((ASPxGridView)sender, "UnitPrice");
}
票数 1
EN

Stack Overflow用户

发布于 2011-03-20 19:53:30

我不知道devexpress,但在常规的网格视图中,您可以轻松地计算总数并显示在页脚上。

代码语言:javascript
复制
 double GrdTotal = 0;
    protected void grdList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        double price = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "UnitPrice"));
        GrdTotal += price;
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Label lblTotalPrice = (Label)e.Row.FindControl("UnitPrice");
            lblTotalPrice.Text = GrdTotal.ToString();
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5370997

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档