Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
90
Calculate selected area like Excel does.
posted

On an Excel sheet, if you selected an area, the bottom status bar will display average, count, sum results of the selected area.

I am using XamDataGrid.

How can I achieve this Excel-like effects in XamDataGrid?

Parents
No Data
Reply
  • 2680
    Offline posted

    Hello,

    I have been looking into your question and since there is no such functionality out of the box, I created a small sample that demonstrates a possible approach to show a bar with average, count and sum results of the selected cells in the XamDataGrid.

    In order to access the selected cells, my suggestion is to handle the XamDataGrids's SelectedItemsChanged event and get them using the grid's SelectedItems.Cells collection:

    private void xamDataGrid1_SelectedItemsChanged(object sender, Infragistics.Windows.DataPresenter.Events.SelectedItemsChangedEventArgs e)
    {
    ...
        SelectedCellCollection scc = (e.OriginalSource as XamDataGrid).SelectedItems.Cells;
    ...
    }

    Having in mind that the non-numeric fields can be counted, but not calculated in the sum and average values, I separate the cells into two different lists depending on whether they are numeric or not. For the purposes of the example, the numeric type is Int32, however, you could modify it according to your scenario:

    foreach (var cell in scc)
    {
        if (Type.GetTypeCode(cell.Field.DataType) == TypeCode.Int32)
            selectedNumericCellsValues.Add(Int32.Parse(cell.Value.ToString()));
        else
            selectedOtherCellsValues.Add(cell.Value.ToString());
    }

    Finally, I apply the aggregated functions to the lists of values and set the result as the text of the corresponding TextBlocks:

    this.countTB.Text = (selectedNumericCellsValues.Count() + selectedOtherCellsValues.Count()).ToString();
    if (selectedNumericCellsValues.Count != 0)
    {
        this.sumTB.Text = selectedNumericCellsValues.Sum().ToString();
        this.averageTB.Text = selectedNumericCellsValues.Average().ToString();
    }

    Of course, you could use labels, text boxes or controls per you requirement where you could display the results.

    I have attached my sample below. Please test it on your side and let me know if I may be of any further assistance.

    Sincerely,
    Bozhidara Pachilova
    Associate Software Developer

    7848.XDGExcelLikeSummary.zip

Children
No Data