This is a migrated thread and some comments may be shown as answers.

DisplayColumnAttribute and RadGridView

1 Answer 103 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Kakone
Top achievements
Rank 1
Kakone asked on 10 May 2010, 05:09 PM
Hello,

I have an entity with a Type property like this (a foreign key) :
public sealed partial class Project : Entity 
    [Display(Name="Type")] 
    public ProjectType Type 
    { 
        get { ... } 
        set { ... } 
    } 

My ProjectType class has a DisplayColumn attribute :
[DisplayColumn("Label")]  
public sealed partial class ProjectType : Entity  
    ... 
    public string Label 
    { 
        get { ... } 
        set { ... } 
    } 
    ...  

I want to autogenerate my columns in a RadGridView, but I didn't succeed for the Type property of my Project class. The value into this column is "ProjectType : PrimaryKey value", not my label.
Is there another way to display a foreign key column or must I forget to autogenerate columns ?

Cordially,
Stephane.

1 Answer, 1 is accepted

Sort by
0
Kakone
Top achievements
Rank 1
answered on 11 May 2010, 08:48 AM
I found a workaround to this problem. I share my idea (if someone is interested): I overrided the OnAutoGeneratingColumn method of the RadGridView :
protected override void OnAutoGeneratingColumn(GridViewAutoGeneratingColumnEventArgs e) 
    if (!e.Cancel) 
    { 
        GridViewBoundColumnBase column = e.Column as GridViewBoundColumnBase; 
        if (column != null
        { 
            Type type = column.DataType; 
            if (type != null
            { 
                foreach (DisplayColumnAttribute attribute in type.GetCustomAttributes(typeof(DisplayColumnAttribute), true)) 
                { 
                    if (!String.IsNullOrEmpty(column.DataMemberBinding.Path.Path)) 
                    { 
                        column.DataMemberBinding = new Binding() 
                        { 
                            Path = new PropertyPath(column.DataMemberBinding.Path.Path + "." + attribute.DisplayColumn) 
                        };
column.DataType = typeof(string); 
                    } 
                    break
                }                       
            } 
        } 
    } 
    base.OnAutoGeneratingColumn(e); 

And it works !!!

Cordially,
Stephane.
Tags
GridView
Asked by
Kakone
Top achievements
Rank 1
Answers by
Kakone
Top achievements
Rank 1
Share this question
or