There is a DataGrid with a known unknown number of columns and rows.
When the application is started, a config file is read and a collection of columns is generated on its basis:

<DataGrid Name="lstInfo" Margin="5" AutoGenerateColumns="False"/> 

  lstInfo.Columns.Add((DataGridColumn)Resources["colDistrict"]); for (int i = 0; i < Queries.Count; ++i) lstInfo.Columns.Add(new DataGridTextColumn { Header = Queries[i].Name, Binding = new Binding($"Values[{i}]") { Mode = BindingMode.OneWay } }); lstInfo.Columns.Add((DataGridColumn)Resources["colProgressBar"]); 

Now there is a task to display the last line of the grid (or, most likely, just under the grid, it doesn’t matter how it goes) the line "TOTAL".
How to do it?

So far, as a temporary solution - I added another DataGrid without headers from below, this solution suits in principle, but now we need to bind the width of the columns to the width of the columns of the upper DataGrid Screenshot

  • What is the difficulty? The value of the "total" amount for one column or for each column its own "total"? - Gardes
  • The difficulty is that the collection is tied to the DataGrid.ItemsSource and add a "foreign" element (which would calculate the sum) is a bad curved solution. A bottom element must be added that would have the same number of columns as in the DataGrid, as well as changing the width of the DataGrid columns and the width of the columns of this element changed. Somewhere on enSO I found a similar solution for ListView / GridView using GridViewRowPresenter, but here the difference is that the number of columns is not known in advance. - Andrey NOP

1 answer 1

As a result, I implemented it all the same in the form of an additional line in the same DataGrid - I wrote the base abstract class and the data lines, the string total inherited from this class. I had to use 2 collections - the first only for data rows, the second for all rows that are output to the DataGrid.

The decision with two DataGrid was rejected because I did not understand how to tie the width of the columns of the second grid to the first one, and it was necessary to do something with horizontal scrolling.