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

[Solved] GridViewDataColumn - Contained objects

2 Answers 630 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Charles
Top achievements
Rank 2
Charles asked on 02 Jan 2009, 06:10 PM
I have a RadGridView with AutoGenerateColumns set to false. I have several GridViewDataColumns that I define for the grid, all with HeaderText set and with different UniqueName fields defined. I am using a collection of objects defined within my application to populate the grid. Each of the UniqueName values corresponds to a property name in the object begin used on a per row basis. However, one of the properties of the objects is really another object with properties of it's own. You can see this below on line 9. I have attempted to use the object name "TaskStatus" along with the property ".Name" - but that doesn't display anything in the grid. Is there any way to get the grid columns to bind correctly to contained objects within the objects within a list sent off to the ItemsSource of the grid?

                <gridView:RadGridView Name="grdTasks" ColumnsWidthMode="Auto" AutoGenerateColumns="False">  
                    <gridView:RadGridView.Columns> 
                        <gridViewExtras:GridViewDataColumn IsReadOnly="True" HeaderText="ID" UniqueName="TaskId"/>  
                        <gridViewExtras:GridViewDataColumn IsReadOnly="True" HeaderText="Name" UniqueName="Name"/>   
                        <gridViewExtras:GridViewDataColumn IsReadOnly="True" HeaderText="Description" UniqueName="Description"/>  
                        <gridViewExtras:GridViewDataColumn IsReadOnly="True" HeaderText="Parent ID" UniqueName="ParentTaskId"/>  
                        <gridViewExtras:GridViewDataColumn IsReadOnly="True" HeaderText="Due" UniqueName="DueDate" DataFormatString=""/>  
                        <gridViewExtras:GridViewDataColumn IsReadOnly="False" HeaderText="Active" UniqueName="IsActive"/>   
                        <gridViewExtras:GridViewDataColumn IsReadOnly="False" HeaderText="Status" UniqueName="TaskStatus.Name"/>  
                    </gridView:RadGridView.Columns> 
                </gridView:RadGridView> 

2 Answers, 1 is accepted

Sort by
0
Charles
Top achievements
Rank 2
answered on 02 Jan 2009, 06:40 PM
Looks like I found the answer through trial and error. Instead of using the UniqueName property when defining the column, I used the DataMemberBinding proeprty and set it to "{Binding TaskStatus.Name}". That seems to work. See the code below (line #9). Also, could someone from Telerik tell us the appropriate way to use these properties of the GridViewDataColumn?. The DataMemberBinding, the DataMemberPath, and so on. I also need to format the date being displayed. I see the DataFormatString property for the GridViewDataColumn, but I've trying to use it as follows: "d", "{d}", "0:d", "{0:d}" - nothing seems to work.

                <gridView:RadGridView Name="grdTasks" ColumnsWidthMode="Auto" AutoGenerateColumns="False">  
                    <gridView:RadGridView.Columns> 
                        <gridViewExtras:GridViewDataColumn IsReadOnly="True" HeaderText="ID" UniqueName="TaskId"/>  
                        <gridViewExtras:GridViewDataColumn IsReadOnly="True" HeaderText="Name" UniqueName="Name"/>   
                        <gridViewExtras:GridViewDataColumn IsReadOnly="True" HeaderText="Description" UniqueName="Description"/>  
                        <gridViewExtras:GridViewDataColumn IsReadOnly="True" HeaderText="Parent ID" UniqueName="ParentTaskId"/>  
                        <gridViewExtras:GridViewDataColumn IsReadOnly="True" HeaderText="Due" UniqueName="DueDate"/>  
                        <gridViewExtras:GridViewDataColumn IsReadOnly="False" HeaderText="Active" UniqueName="IsActive"/>   
                        <gridViewExtras:GridViewDataColumn IsReadOnly="False" HeaderText="Status"  DataMemberBinding="{Binding TaskStatus.Name}" /> 
                    </gridView:RadGridView.Columns> 
                </gridView:RadGridView> 
0
Accepted
Pavel Pavlov
Telerik team
answered on 07 Jan 2009, 01:05 PM
Hi Charles,

The UniqueName property is used to identify the column among others , so you can access it by name rather than by index. If DataMemberBinding is not set, the RadGridView uses the UniqueName property to try find the appropriate field in the data source.

The recommended way is to use the DataMemberBinding property especially when binding to nested properties.

I would not recommend using DataMemberPath property as it would not support all scenarios. We are already considering to mark it obsolete.

On the date formatting issue:
here is a sample XAML:

            <telerik:RadGridView.Columns> 
                <gridView:GridViewDataColumn IsReadOnly="True"  DataMemberBinding="{Binding}" DataFormatString="{}{0:d}" /> 
            </telerik:RadGridView.Columns> 
...
The RadGridView here is bound to a List<DateTime>.

Sincerely yours,
Pavel Pavlov
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
GridView
Asked by
Charles
Top achievements
Rank 2
Answers by
Charles
Top achievements
Rank 2
Pavel Pavlov
Telerik team
Share this question
or