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

2 sujets de 1 à 2 (sur un total de 2)
  • Auteur
    Messages
  • 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]
    Membre
    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;
    }

2 sujets de 1 à 2 (sur un total de 2)
  • Vous devez être connecté pour répondre à ce sujet.