Make multiple DataGrid cells for multiple columns

Well, I have been looking for quite some time to solve this problem. I am developing a simple printing system for DataGrids WPF and managed to print tables with evenly spaced cells using a DataTable and set it as a DataGrid ItemSource.

However, I need some for rows containing only one cell (you can think of it as a "row group header" inside the table).

So, since I did not find anything about DataTable cells spanning multiple columns (if this can be done, it would be great to know how), I decided that I would have to add rows to the DataGrid manually and solve this something like this:

  • Create a new DataGrid with the desired columns
  • Add rows one by one by setting a DataGridCellPanel that spans or does not span rows.

The second point is where I have the problem (if it is correct, that is). I need to add a row to a DataGrid that uses a simple array of rows as cell data (the index in the array must handle the cell index). Is there an easy way to do something like this?

+6
c # wpf
source share
1 answer

So, after we started to do all this, I reached a very pleasant solution.

It’s best and easiest to apply a data pattern to specific rows after loading a DataGrid. So, I stuck to the original idea with DataTables and remembered the indexes that should have changed their template. I just took DataGridRows from these indexes and applied the template on which the custom ItemsPanelTemplate was created, which spans multiple columns.

EDIT: When prompted by Daniel, I include the code.

First of all, we need to create a template for the string:

<ControlTemplate TargetType='{x:Type DataGridRow}' xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' > <Border> <DataGridCellsPresenter Foreground='Black'> <DataGridCellsPresenter.ItemsPanel> <ItemsPanelTemplate> <local:DataGridSpannedCellPanel /> </ItemsPanelTemplate> </DataGridCellsPresenter.ItemsPanel> </DataGridCellsPresenter> </Border> </ControlTemplate> 

NOTE. local: DataGridSpannedCellPanel is a custom DataGridCellsPanel with an overridden ArrangeOverride method that makes the entire cell size of the entire cell.

You can make an encoded string and load a template from it, for example. The following is creating your grid and initializing some lines with this new template:

 var newGrid = MakeNewDataGrid(); newGrid.ItemsSource = myTable.AsDataView(); var template = XamlReader.Parse(HeaderRowTemplate) as ControlTemplate; foreach (int index in myHeaderIndices) { var container = newGrid.ItemContainerGenerator.ContainerFromIndex(index); var row = container as DataGridRow; if (row != null) { row.Template = template; } } 

Also note that the rows in the table should be made as follows:

 if (bindableQuote.IsGroup()) { table.Rows.Add("Header"); } else table.Rows.Add(rowData.ToArray()); 

What about this, it remains only to figure out how to implement the DataGridSpannedCellPanel.

+4
source share

All Articles