« T-Mobile Huawei stick *does* work on Snow LeopardDevExpress Training Dates Available »

Binding the DXGrid to an XPO datasource

09/04/10

Permalink 03:57:46 pm
Categories: General, Programming, .NET, XPO, Developer Express

Binding the DXGrid to an XPO datasource

I found a forum post today about binding an XPO datasource to the DevExpress DXGrid control. I had posted about this on my DX blog before, but that is more than a year ago – things are moving along quickly, and this post is very likely going to be outdated again with 2010.1… but that’s life, isn’t it :-)

So, I thought I should have another look and see what changed and what might be easier. So I started recreating the steps I went through for that other post, with the same data and all, but with the current (2009.3) version of the libraries.

I’ll document the differences I found below, but include all the steps I took for completeness’ sake – there’ll be a bit of replication, but it’s still worth reading the old post anyway!

The first difference that came up is that the GridColumnView is now called TableView. Okay, that’s easy. With that change, the first grid worked with the same XAML as before:

<dxg :GridControl Name="customerGrid" AutoPopulateColumns="True">
  <dxg :GridControl.View>
    <dxg :TableView>
  </dxg>
</dxg>

This is based on the idea that a data source is assigned in the code-behind, which I’m (still) doing like this:

UnitOfWork uow;

protected override void OnInitialized(EventArgs e) {
  base.OnInitialized(e);

  uow = new UnitOfWork();
  customerGrid.DataSource = new XPCollection<customer>(uow);
}

My next step was to try and get the master/detail setup working. Previously this was a bit weird, with the need to extract the DataContext from the current row etc etc. I plugged in my old binding:

DataSource="{Binding ElementName=customerGrid, Path=View.CurrentRowData.DataContext.This.Orders}"

And…. nothing. Hm. A bit more looking around, and it turns out this has simply become much easier. My binding now changes to this and everything works:

DataSource="{Binding ElementName=customerGrid, Path=View.FocusedRow.Orders}"

This is very nice! Not only is it much easier to retrieve the information about the currently selected object, there’s also no problem anymore with intermediate data types with copied properties that aren’t all there… cool.

Moving on, I want to activate XPO Server mode. The basics of this haven’t changed, so I still need to set my datasource to an instance of XPServerCollectionSource:

customerGrid.DataSource = new XPServerCollectionSource(uow, typeof(Customer));

The problem with the “detail” data remains though, and some details have changed for the method of hooking up through the event. The event that fires when the focused row changes is now called FocusedRowChanged, so my XAML now looks like this:

<dxg :GridControl Name="customerGrid" AutoPopulateColumns="True">
  <dxg :GridControl.View>
    <dxg :TableView FocusedRowChanged="TableView_FocusedRowChanged">
  </dxg>
</dxg>

On the other hand, the code behind is now simpler as well, due to the same changes as above:

private void TableView_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e) {
  var selectedCustomer = customerGrid.View.FocusedRow as Customer;
  if (selectedCustomer != null)
    orderGrid.DataSource = new XPServerCollectionSource(uow, typeof(Order), 
      new BinaryOperator("Customer", selectedCustomer));
}

So far, so good. I didn’t go through the steps of automating the collection encapsulation again – they are outlined in the original post and the only differences that will be relevant to that code are those already described above. Overall – some changes, but they are the good kind because they make things simpler. Good work, DX!

Here’s the new version of my demo application, built against DXperience 9.3.3: DXGridXPOBinding.zip

Have fun!

No feedback yet

Leave a comment


Your email address will not be revealed on this site.
(Line breaks become <br />)
(For my next comment on this site)
(Allow users to contact me through a message form -- Your email will not be revealed!)
Please complete the song title below. Hint: enter 's', 'a', 't', 'i', 's', 'f', 'a', 'c', 't', 'i', 'o', 'n'
antispam test

Enter your email address:

Search

Oliver
MVP logo
October 2014
Sun Mon Tue Wed Thu Fri Sat
 << <   > >>
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31