Datagrid ColumnStyle MappingName To SubClass of a Collection

I have a custom collection ProductsCollection that contains products 
objects.
ProductsCollection is derived from the CollectionBase.
Product object has properties:  name and supplier.

Supplier property is another custom object Supplier.
Supplier object has properties:  name and country.

I want to do custom mapping of the ProductsCollection on datagrid 
(System.Windows.Forms.DataGrid).

Her is the pseudocode:

ProductsCollection p = comboBoxCatalog.SelectedItem as ProductsCollection;

DataGridTableStyle style = new DataGridTableStyle();
style.ReadOnly = true;
style.MappingName = p.GetType().Name;// ProductsCollection OK

PropertyDescriptorCollection descriptors = 
this.BindingContext[p].GetItemProperties();

//Product.Name mapping OK **********************************
DataGridColumnStyle productName = new 
DataGridTextBoxColumn(descriptors["Name"]);

productName.MappingName = "Name";
productName.HeaderText = "Product Name";
style.GridColumnStyles.Add(productName);

//OK "descriptor" exists  ****************
DataGridColumnStyle productSupplier = new 
DataGridTextBoxColumn(descriptors["Supplier"]);

//Supplier.Name mapping FAILS **********************************
productSupplier.MappingName = "Supplier.Name";
productSupplier.HeaderText = "Supplier Name";
style.GridColumnStyles.Add(productSupplier);

dataGridCatalogItems.TableStyles.Add(style);

After that my datagrid displays only the "Product Name" column, and the 
mapping for the Supplier columns doesn't exists.
Is it possible to somehow properly map the custom collection that contains 
custom objects (Supplier) to the datagrid column

Thanks in advance

Sanjin 


Answer this question

Datagrid ColumnStyle MappingName To SubClass of a Collection

  • nredman

    There ya go!
  • Otavio

    I don't think you can do that directly...

    One workaround might be to modify the Product Class to expose the Supplier Name as a property if the supplier is set...

    A vb example of this class might look like:

    Public Class Product
        Inherits System.Object

        Private myName As String = ""
        Private mySupplier As Supplier = Nothing

        Public Sub New()
            MyBase.New()
        End Sub

        Public Sub New(ByVal Name As String, ByVal Supplier As Supplier)
            MyBase.New()
            myName = Name
            mySupplier = Supplier
        End Sub

        Public Property Name() As String
            Get
                Return myName
            End Get
            Set(ByVal Value As String)
                myName = Value
            End Set
        End Property

        Public Property Supplier() As Supplier
            Get
                Return mySupplier
            End Get
            Set(ByVal Value As Supplier)
                mySupplier = Value
            End Set
        End Property

        Public Property SupplierName() As String
            Get
                If Not mySupplier Is Nothing Then
                    Return mySupplier.Name
                Else
                    Return ""
                End If
            End Get
            Set(ByVal Value As String)
                'You could just make this property ReadOnly
                If Not mySupplier Is Nothing Then
                    mySupplier.Name = Value
                End If
            End Set
        End Property

    End Class


  • Kadesh

    Thanks :)
    Another approach is to override ToString method of the Supplier.
    I.e.

    Public override string ToString ()
    {
    Return this.Name;
    }

    And in the mapping:
    ...
    productSupplier.MappingName = "Supplier"; 
    productSupplier.HeaderText = "Supplier Name";
    ...

  • Datagrid ColumnStyle MappingName To SubClass of a Collection