Offler Offler - 1 month ago 8
C# Question

Correct way to bind a GridLookUpEdit to a XtraGridColumn

I want to do the following:
I have an XtraGrid (short Grid) on top of an Dialog to choose e.g. offers.
A offer has a dealer from a list of all dealers.
'offer' and 'dealer' are XPO Objects.
In the lower part there is a GridLookUpEdit (short LookUp)

Maybe I'm totally in a wrong way, because we used another data mapper before, but I'm completly stuck.

I have defined an XPCollection which is bound to the Grid and another XPCollection bound to the LookUp.

If I select a row in the Grid, values of Lookups which are not XPO-objects get changed according to the selected row.
As soon as the LookUp contains an XPO Object nothing is displayed. Session of all XPCollections are the same.

If i click in the LookUp, a list of all Dealers is shown.

If i select an entry I do the following:

Order order = gridView.GetRow(gridView.FocusedRowHandle); => shows an order
Dealer actDealer = this.gridLookUpEditDealer.EditValue as Dealer; => shows a Dealer

order.Dealer = actDealer; => lets the selected value vanish.


this.gridLookUpEditDealer.EditValue stays the same, but is no longer displayed.

The DataBinging of the LookUp is:

this.gridLookUpEditDealer.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this.mainGrid, "Dealer", true));
this.gridLookUpEditDealer.Properties.DataSource = this.dealers; // dealers = XPCollection


If i try:

this.gridLookUpEditDealer.EditValue = current.Dealer;


The LookUpEdit shows the value, but also "Value not valid"

I think the root cause is something that maybe the value in the Grid is not correctly bound as value in the LookUp. But what would be a correct way?

Answer

I think the problem is, that you directly binding the whole Dealer. Set up your GridLookUpEdit as described in DevExpress Documentation.

So you should do following for your LookupEdit:

  1. Set the LookupEdit.ValueMember = "DealerId";
  2. Bound the EditValue to this DealerId.
  3. Set the LookupEdit.DisplayMember = "Name"; //Or sth. you like to show
  4. Set your LookupEdit.DataSource = YourDealer-List

With something like this you get your DealerObject back:

Dealer dealer = GridLookUpEdit.Properties.GetRowByKeyValue(lookUpEdit.EditValue) as Dealer;

Because the EditValue will be your DealerId now. Further notice that you can use repositoryGridLookupEdit directly in your Offer Grid. So you don't need more then one Grid to show your information in a usable manner. Each offer is able to has a own Dealer-Column which holds a dealer. The repositoryLookupEdit allows you to edit the dealer within the Grid.

I hope this will help you. If not clarify your problem, i will assist you ;)

Comments