Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I tried to implement your problem in the following way.</p> <p>There is a certain amount of data that must be defined in the columns. But these few, and we want them to define a repeating column in the <code>DataGrid</code>. </p> <p>Suppose it is known, at least <em>approximately</em> the number of columns and rows. Then be the data <code>cube</code> to be set in the <code>DataGrid</code>. Below is a my example with my comments.</p> <p><code>XAML</code></p> <pre><code>&lt;Window x:Class="DataGridDynamicAddCol.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" WindowStartupLocation="CenterScreen" ContentRendered="Window_ContentRendered"&gt; &lt;Grid&gt; &lt;DataGrid Name="DynamicColumnDataGrid" Loaded="DynamicColumnDataGrid_Loaded" AutoGenerateColumns="False"/&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p><code>Code behind</code></p> <pre><code>public partial class MainWindow : Window { // Create class with data // Note: Each parameter contains a list of values for it. public class Person { public Person() { FirstName = new List&lt;string&gt;(); SecondName = new List&lt;string&gt;(); Sample = new List&lt;string&gt;(); } public List&lt;string&gt; FirstName { get; set; } public List&lt;string&gt; SecondName { get; set; } public List&lt;string&gt; Sample { get; set; } } // Number of columns const Int32 COLS = 3; // Number of rows const Int32 ROWS = 3; // Number repeats const Int32 RPTS = 3; // Array of headers string[] HeadersArray = new string[COLS] { "FirstName", "SecondName", "Sample" }; // Array of values: Depends on the number of columns and rows // Note: The number of repetitions you can specify smaller amounts of data // If you specify more, then this place will be empty cells string[, ,] ValuesArray = new string[, ,] { { { "Andy", "Caufmann", "Artist"}, { "Sam", "Fisher", "Spy"}, { "Ben", "Affleck", "Actor"} }, { { "Jim", "Gordon", "Sniper"}, { "Maria", "Gray", "Cleaner"}, { "Katy", "Parry", "Artist"} }, { { "Jack", "Rider", "Hunter"}, { "Sven", "Vath", "DJ"}, { "Paul", "Kalkbrenner", "Super DJ"} } }; private List&lt;Person&gt; OnePerson; public MainWindow() { InitializeComponent(); } private void Window_ContentRendered(object sender, EventArgs e) { OnePerson = new List&lt;Person&gt;(); // Create the columns for (int cols = 0; cols &lt; COLS; cols++) { OnePerson.Add(new Person()); } // Create the rows for (int cols = 0; cols &lt; COLS; cols++) { for (int rows = 0; rows &lt; ROWS; rows++) { OnePerson[rows].FirstName.Add(ValuesArray[cols, rows, 0]); OnePerson[rows].SecondName.Add(ValuesArray[cols, rows, 1]); OnePerson[rows].Sample.Add(ValuesArray[cols, rows, 2]); } } DynamicColumnDataGrid.ItemsSource = OnePerson; } private void DynamicColumnDataGrid_Loaded(object sender, RoutedEventArgs e) { // Create dynamically the columns and rows for (int rpts = 0; rpts &lt; RPTS; rpts++ ) { for (int cols = 0; cols &lt; COLS; cols++) { DataGridTextColumn MyTextColumn = new DataGridTextColumn(); MyTextColumn.Header = HeadersArray[cols]; // Binding values from HeadersArray MyTextColumn.Binding = new Binding(String.Format("{0}[{1}]", new object[] { HeadersArray[cols], rpts } )); // Add column in DataGrid DynamicColumnDataGrid.Columns.Add(MyTextColumn); } } } } </code></pre> <p><code>Output</code></p> <p>The number of repetitions - <code>3</code>:</p> <p><img src="https://i.stack.imgur.com/P7fiL.png" alt="enter image description here"></p> <p>The number of repetitions - <code>2</code>:</p> <p><img src="https://i.stack.imgur.com/O89c6.png" alt="enter image description here"></p> <p>If you specify the number of repetitions is greater than it is in the cube, you will see a picture:</p> <p><img src="https://i.stack.imgur.com/nBzvM.png" alt="enter image description here"></p> <p>That is quite logical, since the relevant data are not <em>available</em>.</p> <p><em><code>Note</code></em>: To use the <code>Binding</code> a <code>ViewModel</code>, you need to determine the appropriate lists / collection, set them to the elements and assemble them into a single data cube. In my example, <em>static</em> data, but you need to determine the source of the data, the amount of data, then set them aside and put them in a data cube. I think, without a data cube, it will be difficult to implement something of this kind (I tried different options, the use of the <code>Converter</code>, <code>Binding</code> directly from the array, etc).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload