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}

    }
}
8
  • Why variable number of columns? Matrix (two-dimentional array) with numbers? Commented Nov 7, 2020 at 11:54
  • Can I add rows to the matrix? Commented Nov 7, 2020 at 12:17
  • Yes. See this ready answer to play with matrix in WPF. But sorry, it's in Russian StackOvetflow Community. Try that code with blank WPF application. Commented Nov 7, 2020 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. gallery.technet.microsoft.com/WPF-Dynamic-XAML-Awkward-41b0689f Commented Nov 7, 2020 at 13:06
  • I think this answered my problem stackoverflow.com/questions/5582226/… Commented Nov 7, 2020 at 14:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.