1

I have a element bound to a Projects object. The Project object has a ClientId property that is linked to a Client object. The Client object has two properties; the Id property which is linked to the ClientId property and a Name property. I can easily display the ClientId from the Project object, but I want to display the Name property from the Client object.

I have tried using a element with a DataSource set to a collection of Clients. I have set the Item* attributes with values from the Clients object. The Value attribute (which is required and what is displayed) cannot see Clients object so I cannot display the value in the Name property in the Client object.

from ViewModel both properties are correctly initialized and filled

public BusinessPackDataSet<MasonAndHangerWebApi.Models.BimCimData.Project> Projects { get; set; }

public IList<MasonAndHangerWebApi.Models.BimCimData.Client> Clients { get; set; }

from View

<bp:GridView DataSource="{value: Projects}" ...>
...         
<bp:GridViewComboBoxColumn DataSource="{value: Clients}"
                                       HeaderText="Client"
                                       ItemKeyBinding="{value: _parent.ClientId}"
                                       ItemTextBinding="{value: Name}"
                                       ItemValueBinding="{value: Name}"
                                       Value="{value: Cannot see values from the Clients datasource }" />
...
</bp:GridView>

What do I need to do to display the Name value from the Client object in this GridView?

2 Answers2

0

I wound up using a Dictionary with the (long)Id in the Key and the string display value in the Value.

<bp:GridViewTextColumn HeaderText="Client"
                                   Value="{value: _root.ClientNames[ClientId]}"
                                   AllowSorting="True" />
0

In general, a better approach is to prepare the objects for the GridView in the exact format you need.

Create a new class which contains all properties the GridView need, and to aviod a lot of code that will transform the data, you can use AutoMapper or a similar library.

public class ProjectDisplayDto
{
    // all properties from the Project class

    public string ClientName { get; set; }

}

In AutoMapper configuration, you just need to create a mapping. The properties which have the same names and types, will be mapped automatically. You only need to define the rules to map the extra ones.

mapper.CreateMap<Project, ProjectDisplayDto>()
    .ForMember(p => ClientName, m => m.MapFrom(p => ...));  // tell AutoMapper how to get the client name

If you are using Entity Framework and Project class is an entity, you'll probably have the Client navigation property present in this class. If the Client has the Name property, AutoMapper will figure out the mapping automatically, so you don't even need to configure the rule for the ClientName property.

It may look like that using this approach a lot of unnecessary classes are created. But in case of larger applications, I see that as an advantage, because often you need to add a column on one screen and having a separate class for that means that you don't affect other screens in the application.

Tomáš Herceg
  • 1,595
  • 1
  • 13
  • 18