2011-03-20 110 views
0

我有一個簡單的DevexpressGridview在Devexpress Gridview中計算小計

enter image description here

這裏代碼;

<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> 

有什麼辦法計算的DevExpress子總Unit Price列?

對於這個例子;我只想在底部總計Unit Price列(18 + 19 + 4.5 + 14 + 18 + 263.5 + 18 + 46 + 14 + 14 + 15 = 430)..

我該怎麼做?

最好的問候,

Soner

回答

1

這可以通過使用ASPxGridView使用以下方法來實現。 2)使用詳細的GridView的DataBound和BeforeGetCallBackResult事件處理程序來設置標籤的文本。這裏是我的代碼:

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"); 
} 
0

我不知道DevExpress的,但在常規的GridView可以很容易地計算總計,並將它們顯示頁腳。

1)在一個單獨的方法計算摘要:

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

@Özgür謝謝,但我想知道我怎麼能做到這一點在ASPxGridview ..應該是不同的正常Gridview。 – 2011-03-20 20:08:51