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

Inheritance class data binding issue

4 Answers 277 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.
Thomas
Top achievements
Rank 1
Thomas asked on 26 Dec 2008, 01:55 AM
When I binding an iList contain base class,the grid show normal.
But when I binding an iList contain Inheritance class,the grid only show Inheritance class field/property,the base class field/property do not show.

4 Answers, 1 is accepted

Sort by
0
Hristo Deshev
Telerik team
answered on 29 Dec 2008, 12:49 PM
Hello Thomas,

RadGridView tries to infer the type of items in the collection it is bound to. The moment it sees an IList<Something> it deduces that Something is the item type, not checking if you actually have InheritedSomething objects inside.

This one is easy to fix -- turn your collection into a typed collection of the inherited type. LINQ makes that dead easy -- if you have an IList<Message>, you can transform it to an IEnumerable<InheritedMessage> using the Cast() extension method:

//will be treated as a collection of Message objects not including properties from InheritedMessage 
//RadGridView1.ItemsSource = GetMessages(); 
 
//will be treated as a collection of InheritedMessage objects 
RadGridView1.ItemsSource = GetMessages().Cast<InheritedMessage>(); 

I am attaching a sample project for your reference.

All the best,
Hristo Deshev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
imusic
Top achievements
Rank 2
answered on 19 Feb 2009, 11:51 AM

I found that not even having collections of inherited type will help with properties defined on base class.

Here is example:

Create C# Silverlight project RadGridInheritance

Add references to:

Telerik.Windows.Controls

Telerik.Windows.Controls.GridView

Telerik.Windows.Controls.Navigation

Add ContactSettings.cs:

using System;

using System.Collections.ObjectModel;

 

namespace RadGridInheritance

{

    public class ContactType

    {

        public string Description { get; set; }

    }

 

    public class AddressType:ContactType

    {

 

        public bool IsPostal { get; set; }

 

    }

 

    public class PhoneType

    {

        public string Description { get; set; }

    }

 

    public class ContactSettings

    {

        public ObservableCollection<ContactType> ContactTypes { get; set; }

        public ObservableCollection<AddressType> AddressTypes { get; set; }

        public ObservableCollection<PhoneType> PhoneTypes { get; set; }

 

    }

}

 

And page.xaml:

<UserControl x:Class="RadGridInheritance.Page"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:telerikInput="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Input"

    xmlns:telerikNavigation="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation"

    xmlns:telerikGridView="clr-namespace:Telerik.Windows.Controls.GridView;assembly=Telerik.Windows.Controls.GridView"

    xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"

    xmlns:telerik2="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls"            

    Width="400" Height="350">

    <Grid x:Name="LayoutRoot" Background="White">

        <Grid.RowDefinitions>

            <RowDefinition />

        </Grid.RowDefinitions>

        <telerikNavigation:RadTabControl Grid.Row="1">

            <telerikNavigation:RadTabControl.ItemsPanel>

                <ItemsPanelTemplate>

                    <telerik2:RadWrapPanel />

                </ItemsPanelTemplate>

            </telerikNavigation:RadTabControl.ItemsPanel>

            <telerikNavigation:RadTabItem Header="Address Types">

                <ScrollViewer VerticalScrollBarVisibility="Auto" BorderThickness="0">

                    <Grid>

                        <telerik:RadGridView

                            x:Name="AddressTypeGrid"

                            ItemsSource="{Binding AddressTypes}"

                            AutoGenerateColumns="False"

                            IsReadOnly="True"

                            Height="300"

                            ColumnsWidthMode="Auto"

                            ShowGroupPanel="False" >

                            <telerik:RadGridView.Columns>

                                <telerikGridView:GridViewDataColumn HeaderText="Description" UniqueName="Description" />

                                <telerikGridView:GridViewDataColumn HeaderText="IsPostal" UniqueName="IsPostal"  />

                            </telerik:RadGridView.Columns>

                        </telerik:RadGridView>

                    

                    </Grid>

                </ScrollViewer>

            </telerikNavigation:RadTabItem>

            <telerikNavigation:RadTabItem Header="Contact Types">

            <ScrollViewer VerticalScrollBarVisibility="Auto" BorderThickness="0">

                <Grid>

                    <telerik:RadGridView

                            x:Name="ContactTypeGrid"

                            ItemsSource="{Binding AddressTypes}"

                            AutoGenerateColumns="False"

                            Height="300"

                            IsReadOnly="True"

                            ColumnsWidthMode="Auto"

                            ShowGroupPanel="False" >

                        <telerik:RadGridView.Columns>

                            <telerikGridView:GridViewDataColumn HeaderText="Description" UniqueName="Description" />

                        </telerik:RadGridView.Columns>

                    </telerik:RadGridView>

 

                </Grid>

            </ScrollViewer>

        </telerikNavigation:RadTabItem>

            <telerikNavigation:RadTabItem Header="Phone Types">

                <ScrollViewer VerticalScrollBarVisibility="Auto" BorderThickness="0">

                    <Grid>

                        <telerik:RadGridView

                            x:Name="PhoneTypeGrid"

                            ItemsSource="{Binding PhoneTypes}"

                            AutoGenerateColumns="False"

                            Height="300"

                            IsReadOnly="True"

                            ColumnsWidthMode="Auto"

                            ShowGroupPanel="False" >

                            <telerik:RadGridView.Columns>

                                <telerikGridView:GridViewDataColumn HeaderText="Description" UniqueName="Description" />

                            </telerik:RadGridView.Columns>

                        </telerik:RadGridView>

 

                    </Grid>

                </ScrollViewer>

            </telerikNavigation:RadTabItem>

        </telerikNavigation:RadTabControl>

    </Grid>

</UserControl>

 

When you run the project you will notice that Description column on first 2 tabs displays no information.

 

0
imusic
Top achievements
Rank 2
answered on 20 Feb 2009, 11:37 AM
Hristo,

example you have posted is WPF. When I run that code in Silverlight I get only Headers column.

As far as I can see issue is that grid can't be bound to properties defined in the base class as Thomas mentioned. This also affects column autogenaration.
0
Hristo Deshev
Telerik team
answered on 23 Feb 2009, 12:26 PM
Hi imusic,

I've reproduced the bug. It appears on Silverlight only, and we will fix it for our next release. Thanks for reporting that! I have updated your Telerik points.

Best wishes,
Hristo Deshev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
GridView
Asked by
Thomas
Top achievements
Rank 1
Answers by
Hristo Deshev
Telerik team
imusic
Top achievements
Rank 2
Share this question
or