0

I have an object that has two members, one is a string and the other is an array of ushort. I need to display each element in the array in a different column.

<DataGrid ItemsSource="{Binding Results}" AutoGenerateColumns="True" /> 

The datagrid is binded to a viewmodel:

public class ViewModel
{
     public ObservableCollection<ResultView> Results
     {
        get;
        set;
     }
}

public class ResultView
{
     public string From {};
     public ushort[] Data {};
}

Edit:

There are no errors, what is happenning now is that I change the getter of the ushort array to return one big string that displays all the elements, but I want each element to be displayed in a different column.

Matan Givoni
  • 1,080
  • 12
  • 34

2 Answers2

0

Make sure that the DataContext of your DataGrid is the ViewModel. If that's the case. You have to update your code similar to

public class ViewModel 
{
     private ObservableCollection<ResultView> _results;
     public ObservableCollection<ResultView> Results
     {
        get { 
        if(_results == null)
        { 
          _results = new ObservableCollection<ResultView>() { new ResultView() { From = "Sample 1" } };
        }  
         return _results; 
        }
     }
}
123 456 789 0
  • 10,565
  • 4
  • 43
  • 72
  • Everything is working fine I can see the results on the grid, thia is not my actuall code this is only a minimized code. My wish ia to display each element of the array of ResultView in a different column. And yes the datacontext of the grid is the viewmodel because its inherated from the root element. – Matan Givoni Jan 16 '14 at 07:43
0

I change the getter of the ushort array to return one big string that displays all the elements

The problem with this is that representing the array of ushorts's as a single string will result in the values being displayed in one column, as a string is a single variable.

If you have done this because you have had trouble display in the numeric values themselves, then this can be resolved.

However if it is your wish to display them as a string you will need to store a collection of strings, or char could work.

AutoGenerateColumns may not work how you perceive it. This will display any and all properties it has access to, but won't iterate through an array, for example, and can't generate columns for these.

Instead of regurgitation the answer, I recommend looking at his:

How can I display array elements in a WPF DataGrid?.

I hope this helps.

Community
  • 1
  • 1
tjheslin1
  • 1,378
  • 6
  • 19
  • 36