Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1060
Binding Grid to List of Entity
posted

I have a grid bound to a list of Entities.

For the sake of this question, let`s say my entity contains 3 fields

Int ID

Product Product

Decimal Quantity

Product contains 2 field

string Key

string Description

When I bind my grid to list of Entities

I see 3 columns... Id, Quantity are good, but Product gives me Projet1Name.Classes.Product when I would want the description to show.

I created a UltraComboEditor

I set its datasource to a list of Products

I set DisplayMember to Description and ValueMember to Key.  

I then set in the initializeLayout the EditorComponent of the Column to the created ultracomboeditor

the combo works if i click the dropdown button i see my products.. but i cant choose something, i get 

Unable to update the data value:  Object of type System.DBNull cannot be converted to type Products.

I added 

e.Layout.Bands[0].Columns["Activite"].Nullable = Infragistics.Win.UltraWinGrid.Nullable.Nothing;

in the initializeLayout.. error is gone, but after chosing a value.. the comboeditor is blank.. telling me the id is not matched.

I figured out that i can add a extra field to my entity, productKey, and bind to that column, but its an extra step to i would prefer not have to make

I am probably close..... but i am stuck

thanks in advance.

F.

Parents
  • 469350
    Verified Answer
    Offline posted

    Hi,

    The grid cell will call the ToString method of the cell's value (the Product in this case). The default implementation of ToString in the DotNet Framework is to show the class name. So one very easy thing you could do here is override ToString on your Product class and return whatever text you want.

    As for the combo, the grid will try to match up the cell's value (Product) with the ValueMember of the item on the list (which you are assigning the Key property). So that won't work because the object instance does not equal the key (which is presumably a string). So if you want the product field to be editable, you have a number of options.

    One thing you could do is change your data structure so that the root level Product field is a numeric field that just contains the key instead of the Product object. That makes sense, anyway, since the way you have it now, your contain will contain duplicate instances of the same Product object for every row that uses that product.All the root-level needs is the key and then it can look up the project in another table.

    Another option would be to set the ValueMember on the Combo to a field that contains the entire instance of the Product and not just the key. But this is tricky, because even if you do that, the instance of the Product in the grid cell will not be the same instance as the Product in the combo cell. So you would have to override the Equals (and possibly GetHashCode) method on Product so that it compares based on key and not instance.

Reply Children
No Data