0

I want to make a datagrid with a varaible number of colums, which set after the config file is loaded. Like the example from wpf-tutorial but with variable number of extra atributes as array but this is not working:

    using System;
using System.Collections.Generic;
using System.Windows;

namespace WpfTutorialSamples.DataGrid_control
{
    public partial class DataGridColumnsSample : Window
    {
        public DataGridColumnsSample()
        {
            InitializeComponent();

            List<User> users = new List<User>();
            users.Add(new User() { Id = 1, Name = "John Doe", Birthday = new DateTime(1971, 7, 23),ExtraAttribute=new int[4] });
            users.Add(new User() { Id = 2, Name = "Jane Doe", Birthday = new DateTime(1974, 1, 17) ,ExtraAttribute=new int[4]});
            users.Add(new User() { Id = 3, Name = "Sammy Doe", Birthday = new DateTime(1991, 9, 2),ExtraAttribute=new int[4] });

            dgUsers.ItemsSource = users;
        }
    }

    public class User
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public DateTime Birthday { get; set; }
        
        public int[] ExtraAtribute {get;set}

    }
}
Yann
  • 67
  • 9
  • Why variable number of columns? Matrix (two-dimentional array) with numbers? – aepot Nov 07 '20 at 11:54
  • Can I add rows to the matrix? – Yann Nov 07 '20 at 12:17
  • Yes. See [this](https://ru.stackoverflow.com/a/1115342/373567) ready answer to play with matrix in WPF. But sorry, it's in Russian StackOvetflow Community. Try that code with blank WPF application. – aepot Nov 07 '20 at 12:31
  • If you want to retain the array you could build the datagrid columns dynamically. You can then use [i] binding where i is an integer index of the array. The sample here does that sort of thing. With rather more complicated data. https://gallery.technet.microsoft.com/WPF-Dynamic-XAML-Awkward-41b0689f – Andy Nov 07 '20 at 13:06
  • I think this answered my problem https://stackoverflow.com/questions/5582226/how-can-i-display-array-elements-in-a-wpf-datagrid – Yann Nov 07 '20 at 14:18
  • I'm sorry that you've ignored my suggestion with complete code. Is that only because of translation lack? – aepot Nov 07 '20 at 14:41
  • Sorry aepot, yes I didn't manage to run it and could not understand the explanation. – Yann Nov 07 '20 at 14:55
  • I can translate it to the answer if you want to try. @Yann – aepot Nov 07 '20 at 14:57

0 Answers0