Home Forums WPF controls Xceed DataGrid for WPF Help with grouping/sorting unbound data

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #23180 |

    Hello,

    I’ve been tring to get something simple to work for a while now and I
    just haven’t succeeded. There is a section on this particular thing in
    your help documentation but it does not provide a good answer. All I am wanting to do is have a collection
    of unbound data items and allow them to be grouped (by dragging the
    column names into the group area) and sorted by clicking on column names. That’s it. Right now it does not let me drag them.
    Your help documentation says, under “Handling Unbound Data”:

    “In order to provide unbound data with grouping, sorting, and filtering
    capabilities, an observable collection containing the unbound data must
    be created and wrapped in a DataGridCollectionView, which can theb be
    set to the grid’ ItemsSource property (See the Providing Unbound Data
    section in Providing Data).”

    I have got it to work with bounded data (like all the samples), but not with unbounded data.
    I am not understanding how to do it and the documentation does not seem
    to provide an example of how to group unbounded data items. I don’t want to be using any of the sample data that comes with the DataGrid. My main overall purpose is to be able to define a record structure of whatever data and whatever columns I want and be able to group and sort that data. Could you perhaps provide an example of being able to group an unbound collection?
    Thanks!

    Imported from legacy forums. Posted by Brad (had 7628 views)

    Xceed Support
    Member
    Post count: 5658

    Hello,

    We are currently working on adding unbound support to the DataGridCollectionView[Source] classes; however, for the moment, your best bet is to add your items to an ObservableCollection that is wrapped by the DataGridCollectionView to which the grid is bound. This will provide all the same grouping, sorting, and filtering features that are available when the grid is bound to, let’s say, a DataTable. For example:

    <code>
    System.Collections.ObjectModel.ObservableCollection<Person> persons = new System.Collections.ObjectModel.ObservableCollection<Person>();
    persons.Add( new Person( “Odi”, “Kosmatos”, Occupation.Management ) );
    persons.Add( new Person( “Michel”, “Fortin”, Occupation.Developer ) );
    persons.Add( new Person( “Pascal”, “Bourque”, Occupation.Developer ) );
    ….
    persons.Add( new Person( “David”, “Redknap”, Occupation.Writer ) );

    DataGridCollectionView view = new DataGridCollectionView( persons );
    DataGridControl grid = new DataGridControl();

    grid.ItemsSource = view;
    this.Content = grid;
    </code>

    Imported from legacy forums. Posted by Jenny [Xceed] (had 458 views)

    User (Old forums)
    Member
    Post count: 23064

    How do I define the columns of the grid so that it works with this Person object?

    Imported from legacy forums. Posted by Brad (had 397 views)

    Xceed Support
    Member
    Post count: 5658

    The Person object that I use to populate the ObservableCollection defines a FirstName, LastName, and Occupation properties. These properties are automatically “read” by the DataGridCollectionView to create the necessary columns.

    If you want to limit the columns that are displayed in a grid, then you will need to use the DataGridCollectionView’s <a href=”http://doc.xceedsoft.com/products/XceedWpfDataGrid/Xceed.Wpf.DataGrid~Xceed.Wpf.DataGrid.DataGridCollectionView~ItemProperties.html”>ItemProperties</a&gt; collection. The ItemProperties collection contains a list of <a href=”http://doc.xceedsoft.com/products/XceedWpfDataGrid/Xceed.Wpf.DataGrid~Xceed.Wpf.DataGrid.DataGridItemProperty.html”>DataGridItemProperty</a&gt; objects that can be added or removed and that contain information relative to their corresponding item (you could consider these items to be the columns that will end up in the grid). The DataGridItemProperty documentation page contains an example of how to use the DataGridItemProperty objects to limit the columns that are displayed in a grid. Although the example uses bound data, the same logic and technique applies.

    Imported from legacy forums. Posted by Jenny [Xceed] (had 220 views)

    User (Old forums)
    Member
    Post count: 23064

    Thank you.

    Imported from legacy forums. Posted by Brad (had 384 views)

    Xceed Support
    Member
    Post count: 5658

    Not a problem. I will add more unbound examples in the upcoming version of the documentation.

    Imported from legacy forums. Posted by Jenny [Xceed] (had 394 views)

    User (Old forums)
    Member
    Post count: 23064

    How do I specify the order of the columns as defined by the properties?

    Imported from legacy forums. Posted by Brad (had 396 views)

    User (Old forums)
    Member
    Post count: 23064

    programmatically…

    Imported from legacy forums. Posted by Brad (had 392 views)

    Xceed Support
    Member
    Post count: 5658

    The order you add them in is the order in which they will be displayed.

    Imported from legacy forums. Posted by Jenny [Xceed] (had 211 views)

    User (Old forums)
    Member
    Post count: 23064

    But the columns are created as a result of defining properties (of the Person class in this case). They do not show up in the order I define them… I defined properties called “Item1”, “Item2”, and “Item3” and now matter what order I declare them in the columns show up in the order: “Item3”, “Item2”, “Item1”

    Imported from legacy forums. Posted by Brad (had 426 views)

    Xceed Support
    Member
    Post count: 5658

    Hmmm… you are correct. Let me check with one of the developers and I will get back to you.

    Sorry!

    Imported from legacy forums. Posted by Jenny [Xceed] (had 287 views)

    Xceed Support
    Member
    Post count: 5658

    OK. What is happening is that as soon as the DataGridCollectionView is created, the default item properties are created. What you will need to do is first clear the ItemProperties collection and them add the DataGridItemProperty objects. The order in which you add them is the order in which they will be displayed. For example:

    <code>
    view.ItemProperties.Clear();
    view.ItemProperties.Add( new DataGridItemProperty( “FirstName”, “FirstName”, typeof( string ) ) );
    view.ItemProperties.Add( new DataGridItemProperty( “LastName”, “LastName”, typeof( string ) ) );
    view.ItemProperties.Add( new DataGridItemProperty( “Occupation”, “Occupation”, typeof( Occupation ) ) );

    </code>

    The developer is checking to see if this should be the normal behavior
    (adding DataGridItemProperty objects that already exist).

    Hope this helps!

    Imported from legacy forums. Posted by Jenny [Xceed] (had 279 views)

    User (Old forums)
    Member
    Post count: 23064

    This adds the columns correctly. Just have to be sure to use the same name as the properties you define. Otherwise your data won’t show up! Thanks for your prompt responses.

    Imported from legacy forums. Posted by Brad (had 8759 views)

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