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?
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()); }
this.countTB.Text = (selectedNumericCellsValues.Count() + selectedOtherCellsValues.Count()).ToString(); if (selectedNumericCellsValues.Count != 0) { this.sumTB.Text = selectedNumericCellsValues.Sum().ToString(); this.averageTB.Text = selectedNumericCellsValues.Average().ToString(); }
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 PachilovaAssociate Software Developer
7848.XDGExcelLikeSummary.zip