Ticket T266518
Visible to All Users

DataLayoutControl - Databinding Problem with LookupEdit and Complex Object

created 10 years ago (modified 9 years ago)

I have a DataLayoutControl on a Winform bound to a bindingsource.  The datasource property is set to a custom object below:

C#
public class Complaint : BusinessBase<Complaint> { // complex object property public WagePaymentMethod PaymentMethod { get { return GetProperty(paymentMethodProperty); } set { SetProperty(paymentMethodProperty, value); } }

One of my fields on the DataLayoutControl is set to a LookUpEdit which is bound to a different BindingSource and that datasource property on that binding source is set to a list of PaymentMethod objects so that the dropdown shows a list of payment methods (Hourly, Monthly, etc).   The dropdown works and shows the description field of each PaymentMethod objects.  When I select one (ex: Hourly), it places the text in the box and when I tab off of it, I get an casting error

Unable to cast object of type 'Labor.WageAndHour.BusinessLayer.WagePaymentMethod' to type 'System.String'

I have set the ValueMember property to null thinking that the binding mechanism would try to place the entire object (WagePaymentMethod) from the dropdown into the Complaint.ClaimantWagePaymentMethod property but this is not the case.

I need know how to tell your binding mechanism to assign the entire selected object in the dropdown to the the property which is a complex object so that I don't get a casting error?

Answers approved by DevExpress Support

created 10 years ago

Hi,

If you leave the ValueMember property empty, LookUpEdit's edit value will be a whole underlying object. If the LookUpEdit.EditValue property is bound to the PaymentMethod property while LookUpEdit's data source is a list of WagePaymentMethod objects, everything should work as expected. I have attached a project to illustrate this. Please modify it to show the issue.

I look forward to your response.

    Show previous comments (5)

      You are exactly correct.  My Complaint.WagePaymentMethod property was loaded from the database and the list of all possible WagePaymentMethods were retrieved and bound to the LookUpEdit dropdown.  The problem was that the control could not find the current WagePaymentMethod in the list because when evaluating whether they were equal, they were not because they were not the same object.  All I had to do was override the Equals method and tell it how to determine if the objects were logically equal and then it worked great.
      Thanks
      Mark

        Okay I have one more question related to databinding a property that is a complex object and required attributes.
        I have the property defined as this…

        C#
        [Required()] public WagePaymentMethod ClaimantWagePaymentMethod { get { return GetProperty(wagePaymentMethodProperty); } set { SetProperty(wagePaymentMethodProperty, value); } }

        However, the dxErrorProvider is not enforcing this required attribute for the LookUpEdit control bound to this.  Why?  Is there a trick to get that type of scenario to work?

        Sasha (DevExpress Support) 9 years ago

          Hello Mark,
          From what I gather, the initial issue has been resolved. To avoid mixing several questions in one thread, I've created a separate ticket for your question about DxErrorProvider. I will answer you in the DxErrorProvider - How to use it for properties marked with the 'Required' attribute thread.

          Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

          Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.