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

RadGridView binding to collection with inherited classes -> bug?

1 Answer 47 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Matt Meleski
Top achievements
Rank 1
Matt Meleski asked on 01 Dec 2009, 06:27 PM

Hi, I have encountered a problem binding to collections that contain an inherited class.

First I have a Class called Employee: See below:

 public class Employee  
    {  
        public int EmployeeId { getset; }  
        public DateTime DateHired { getset; }  
    } 

Next I have a class that inherits from Employee. See below:

public class AccountingEmployee : Employee  
    {  
        public string AccountingFunction { getset; }  
        public int AccountingId { getset; }  
    }  
 

I then have a Silverlight UserControl, where in the code behind, I create two separate Collections to bind to. Collection of Employee Base Class and Collection of the child AccountingEmployee Class See below:
public List<Employee> EmployeeListToBindToBase { getset; }  
            EmployeeListToBindToBase = new List<Employee>();  
            EmployeeListToBindToBase.Add(new AccountingEmployee {EmployeeId = 123 ...  
            EmployeeListToBindToBase.Add(new AccountingEmployee {EmployeeId = 456 ...  
 
            public List<AccountingEmployee> AccountingEmployeeListToBindTo { getset; }      
            List<AccountingEmployee> AccountingEmployeeListToBindTo = new List<AccountingEmployee>();  
            AccountingEmployeeListToBindTo.Add((new AccountingEmployee {EmployeeId = 123 ...   
            AccountingEmployeeListToBindTo.Add((new AccountingEmployee {EmployeeId = 456 ...  

In my control, I have a RadViewGrid. For this  grid,  the ItemSource Binds to:  EmployeeListToBindToBase 
Also in my control, I also have a ItemSource Source Control whose ItemSource Binds to EmployeeListToBindToBase

<telerikGridView:RadGridView                    
   AutoGenerateColumns="False"   
   ItemsSource="{Binding EmployeeListToBindToBase}" ...  
           
<ItemsControl   
   ItemsSource="{Binding EmployeeListToBindToBase}" ... 

The RadGridView contains columns that binds to all the fields in the Base Class and Inherited Class. See below:

 
<telerikGridView:GridViewDataColumn  DataMemberBinding="{Binding AccountingFunction}"/>  
        <telerikGridView:GridViewDataColumn  DataMemberBinding="{Binding AccountingId}"/>  
        <telerikGridView:GridViewDataColumn  DataMemberBinding="{Binding DateHired}"/>  
        <telerikGridView:GridViewDataColumn  DataMemberBinding="{Binding EmployeeId}"/> 

The item template for the ItemsControl, also has controls, that bind to all the fields in the Base Class and Inherited Class. See Below:

<DataTemplate x:Key="MyAccountingEmployeeTemplate">  
            <StackPanel > 
                    <TextBlock  Text="{Binding AccountingFunction}"></TextBlock> 
                    <TextBlock  Text="{Binding AccountingId}"></TextBlock> 
                    <TextBlock  Text="{Binding DateHired}"></TextBlock> 
                    <TextBlock  Text="{Binding EmployeeId}"></TextBlock> 
            </StackPanel> 
        </DataTemplate> 

But, when I run the small test app:  
for the RadGridView: the values for AccountingFunction and AccountingId (ie. the fields in the inherited class) do not show up. But the Base Class Field Values do show up (ie.  DateHired and EmployeeId).
For the ItemsControl: All the values for all four fields show up.
There are no binding errors in the output window in visual studio for missing values in the grid (ie AccountingFunction and AccountingId . I have also placed in ValueConvertors for the RadGridView AccountingFunction and AccountingId Columns, and when I debug through the ValueConvertor code, I can actually see the value being passed in, but ultimately the values for these filelds do not show up in the grid. Therefore, the correct values are being picked up, but they still for some reason, do not show up in the grid.
I am thinking this is a bug, because it DOES work for the ItemsControl, that is binding the same collection.

I also have a separate RadGrdiView grid that is binding to ->  public List<AccountingEmployee> AccountingEmployeeListToBindTo { get; set; }   , all the values are showing up, but I do need to bind to : public List<Employee> EmployeeListToBindToBase { get; set; }, as this is what the service layer is returning. 

I have also attached the small sample app, that reproduces the bug. For some reason, it takes a few minutes for the solution in the zip file (rename the file telerikbinding.jpg to telerikbinding.zip to open in Visual Studio). There is also a attached screenshot (missingvalues.jpg) to illustrate as to what I was talking about above.

Thanks,

Matt

1 Answer, 1 is accepted

Sort by
0
Stefan Dobrev
Telerik team
answered on 03 Dec 2009, 07:50 AM
Hi Matt,

We were able to reproduce the problem you have described.

Let me briefly say what is going on here. When you bind the grid to a generic collection of some type we resolve the element type based on the generic type parameter. However in some cases (as yours) this is not the correct behavior. We should resolve the element type based on the actual elements in the collection.

The good news is that we are already working on a fix that will be available in the next service pack release as well as the next internal build.

Hope this helps,
Stefan Dobrev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
General Discussions
Asked by
Matt Meleski
Top achievements
Rank 1
Answers by
Stefan Dobrev
Telerik team
Share this question
or