Tuesday, 20 December 2016

Data not displaying in Datagrid but columns do when using C#, WPF, XAML and a Struct collection for the itemsource

In the ossAccounts system I had the following local struct defined to make it easier to display some information (linq left outer join) on a DataGrid.

        public struct ListItem
        {
            public int              OrganisationAnalysisCodeId;
            public string         Code;
            public bool           Used;
            public DateTime  StartDate;
            public string         StringValue;
            public float           NumberValue;
            public bool           BoolValue;
        }

While the DataGrid could see the columns and the number of rows all the cells remained empty!

After a day trying everything I could think of I eventually found that adding the {get; set;} to the end of each member sorted out the problem.

        public struct ListItem
        {
            public int              OrganisationAnalysisCodeId  {get; set;}
            public string         Code  {get; set;}
            public bool           Used  {get; set;}
            public DateTime  StartDate  {get; set;}
            public string         StringValue  {get; set;}
            public float           NumberValue  {get; set;}
            public bool           BoolValue  {get; set;}
        }

I am guessing that it has something to do with accessibility levels but that is purely a guess.

Anyway, I hope this will spare someone from hours of headbanging.


No comments: