Search code examples
c#gridviewxtragriddevexpress-windows-uigroup-summaries

custom summerType in xtraGrid gridControl


How can add a custom summerType in xtraGrid gridControl?

I would like to add a SummerItem to a column in my xtraGrid gridControl named total percent whitch will calculate the percentage of the two other columns.

In total I have 3 columns 1. Quantities of Items A 2. Total Quantities and 3. Percentage

Also I have summaryItems with

1. Sum of column 1 (`Quantities of Item A`)
2. Sum of column 2 (`Total Quantities`) and 
3. Total Percentage whitch I would like to make a divition with ( column 1 / column 2 ) * 100

My question is how can I do this? I should use a Custom Summary Type ? if yes how can use this type?

Can anyone help me?

Thanks


Solution

  • I found the solution here https://documentation.devexpress.com/#windowsforms/DevExpressXtraGridViewsGridGridView_CustomSummaryCalculatetopic

    Works perfect for me

    I create two private variables in my class

    private decimal _sumOfValues = 0;
    private decimal _sumOfTotalValue = 0;
    

    Created a custom summary type in the percentage column and in option Tag typed percentageColumnCustomSummary whitch is the ID of this summary column

    Create an event on my xtraGrid

    private void allocationGridView_CustomSummaryCalculate(object sender, DevExpress.Data.CustomSummaryEventArgs e) 
    

    And typed bellow code

    private void allocationGridView_CustomSummaryCalculate(object sender, DevExpress.Data.CustomSummaryEventArgs e) 
            {
                try
                {
                    //int summaryID = Convert.ToInt32((e.Item as GridSummaryItem).Tag);
                    string summaryTag = Convert.ToString((e.Item as GridSummaryItem).Tag);
                    GridView View = sender as GridView;
    
                    // Initialization 
                    if (e.SummaryProcess == CustomSummaryProcess.Start) {
    
                        _sumOfValues = 0;
                        _sumOfTotalValue = 0;
                    }
    
                    //Calculate
                    if (e.SummaryProcess == CustomSummaryProcess.Calculate) {
    
                        decimal colValueColumnValue = Convert.ToDecimal( View.GetRowCellValue(e.RowHandle, "Value") );
                        decimal colTotalValueColumnValue = Convert.ToDecimal( View.GetRowCellValue(e.RowHandle, "TotalValue") );
    
                        switch (summaryTag) {
                            case "percentageColumnCustomSummary":
                                _sumOfValues += colValueColumnValue;
                                _sumOfTotalValue += colTotalValueColumnValue;
                                break;
                        }
                    }
    
                    // Finalization 
                    if (e.SummaryProcess == CustomSummaryProcess.Finalize) {
                        switch (summaryTag) {
                            case "percentageColumnCustomSummary":
                                e.TotalValue = 0;
                                if (_sumOfTotalValue != 0) {
                                    e.TotalValue = (_sumOfValues / _sumOfTotalValue);
                                }
    
                                break;
                        }
                    }  
                }
                catch (System.Exception ex)
                {
                    _logger.ErrorException("allocationGridView_CustomSummaryCalculate", ex);
                }
    
            }
    

    This works great!