Home Forums WPF controls Xceed DataGrid for WPF Create DataGrid columns from dynamic type (ExpandoObject)

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • pote
    Participant
    Post count: 1
    #44286 |

    Hi

    I’m trying to have a DataGrid create columns based on an ItemSource which is a list of dynamic (ExpandoObject) objects.
    Is this even possible? And if so, how?

    Regards
    Paw Baltzersen

    Fawzi [Xceed]
    Member
    Post count: 722

    Hi,

    The only way for the ExpandoObject to work with the grid is to wrap your data source around a DataGridCollectionView, and then create each corresponding ItemProperty.

    For example, you could do the following:

    private void Window_Loaded( object sender, RoutedEventArgs e )
    {
    // Create a dynamic list
    var lstItems = new List<ExpandoObject>();

    // loop to make 5 items
    for( var i = 0; i < 6; i++ )
    {
    dynamic myItem = new ExpandoObject();
    myItem.FirstName = “Todd” + i;
    myItem.LastName = “Krueger” + i;

    lstItems.Add( myItem );
    }

    DataGridCollectionView view = new DataGridCollectionView( lstItems );
    view.ItemProperties.Add( new DataGridItemProperty( “FirstName”, typeof( string ) ) );
    view.ItemProperties.Add( new DataGridItemProperty( “LastName”, typeof( string ) ) );

    // Now bind the grid to the list
    dgvTest.ItemsSource = view;
    }

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.