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

GridView Filtering and Grouping issue

3 Answers 130 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Glen
Top achievements
Rank 1
Glen asked on 02 Jun 2009, 07:47 AM
Hi,

Please bear with me as I am a newbie to WPF and we are evaluating RadGridView as part of a new development project. I have a issue with filtering and grouping on columns.

I have another Grid with Contact information with and a related table that stores other contact information, such as address details. If I have the Grid bound to a datarecord type of Contact and want to have related columns such as Address.Line1, Address.Line2 displayed on the parent GridRow. What is the best way to acheive this? I can get the information display but my issue is that I can not use the Filter or Groups on the Address columns. I have attached some simple code behind that I am using, Name works as expected, however Address.Line1 displays data but can not use filtering or the grouping functionality

private void SetupGridView()  
{  
    myGridView.Columns.Add(new GridViewDataColumn()   
    {  
        HeaderText = "Name",  
        UniqueName = "Name",  
        DataMemberBinding = new Binding("Name")   
    });  
 
    myGridView.Columns.Add(new GridViewDataColumn()  
    {  
        HeaderText = "Address Line 1",  
        UniqueName = "Address.Line1",  
        DataMemberBinding = new Binding("Address.Line1"),  
        IsFilterable = true 
    });  

Any help is greatly appreciated.

Glen

3 Answers, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 04 Jun 2009, 11:22 AM
Hi Glen,

In order to be able to group and sort a column that is bound to such composite property, you need to specify the DataType of the underlying field, which I guess in your case will be string. Here is a small sample how to do this in XAML:

<Window x:Class="Ticket216574_FilterAndGroupByCompositeProperty.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" 
    xmlns:System="clr-namespace:System;assembly=mscorlib"
    <Grid> 
        <telerik:RadGridView Name="RadGridView1" AutoGenerateColumns="False"
            <telerik:RadGridView.Columns> 
                <telerik:GridViewDataColumn HeaderText="First Name" UniqueName="FirstName"  
                        DataMemberBinding="{Binding FirstName}"/> 
                <telerik:GridViewDataColumn HeaderText="Last Name" UniqueName="LastName"  
                        DataMemberBinding="{Binding FirstName}" /> 
                <telerik:GridViewDataColumn HeaderText="Address Line 1" UniqueName="Address.Line1" 
                        DataMemberBinding="{Binding Address.Line1}"  
                        DataType="{x:Type System:String}" /> 
                <telerik:GridViewDataColumn HeaderText="Address Line 2" UniqueName="Address.Line2" 
                        DataMemberBinding="{Binding Address.Line2}" 
                        DataType="{x:Type System:String}" /> 
            </telerik:RadGridView.Columns> 
        </telerik:RadGridView> 
    </Grid> 
</Window> 
 

If you prefer to create your columns in the code-behind you should do it like this:

            myGridView.Columns.Add(new GridViewDataColumn()   
            {   
                HeaderText = "Address Line 1",   
                UniqueName = "Address.Line1",   
                DataMemberBinding = new Binding("Address.Line1"),   
                DataType = typeof(string
            });   
 

I have attached a small sample project that demonstrates that so you can use it as a reference. Please, let us know if you have any other questions or difficulties. We would be glad to help.

Greetings,
Ross
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.
0
Glen
Top achievements
Rank 1
answered on 05 Jun 2009, 12:07 AM
Hi,

Thanks for the reply, I have tried specifying the Data Type which does work but we have run into another issue:

We are using a converter on one of our columns to which basically displays different information based on a default contact type. So if the default contact type is say Home Phone it would display Home Phone but if it was set to Business Phone it would show the Business Phone etc.

XAML Code:

<telerik:GridViewDataColumn HeaderText="Contact Details"   
                                                UniqueName="Contact Details" 
                                                DataMemberBinding="{Binding Path=.,Converter={StaticResource ConatctDisplayConverter}}" 
                                                DataType="{x:Type System:String}" 
                                                IsGroupable="False"/> 

Converter Code:

public class ConatctDisplayConverter : IValueConverter  
    {  
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)  
        {  
            IDefaultModeOfContact obj = null;  
            if (value is Organisation)  
                obj = (Organisation) value;  
            if (value is Contact)  
                obj = (Contact) value;  
            if (obj == null)  
                return null;  
 
            var rv = "";  
            // add additional things to IDefaultModeOfContact  
            switch (obj.DefaultModeOfContact.Description)  
            {  
                case "Home Phone":  
                    rv = obj.HomePhone1;  
                    break;  
                case "Home Phone 2":  
                    rv = obj.HomePhone2;  
                    break;  
                case "Home Fax":  
                    rv = obj.HomeFax1;  
                    break;  
                case "Business Phone":  
                    rv = obj.BusinessPhone1;  
                    break;  
                case "Business Phone 2":  
                    rv = obj.BusinessPhone2;  
                    break;  
                case "Business Fax":  
                    rv = obj.BusinessFax1;  
                    break;  
                case "Email (Home))":  
                    rv = obj.EmailHome;  
                    break;  
                case "Email (Business)":  
                    rv = obj.EmailBusiness;  
                    break;  
                case "Mobile":  
                    rv = obj.Mobile;  
                    break;  
            }  
            return rv;  
        }  
 
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)  
        {  
            throw new NotImplementedException();  
        }  
    } 

The issue here is that if we try and click on the filter we get a message saying that: '' is not a member of type 'DataModels.Organisation'

The grid Itemsource is bound to a List of DataModels.Organisation, should I be using GridViewColumn instead of GridViewDataColumn?

Regards,
Glen


0
Rossen Hristov
Telerik team
answered on 05 Jun 2009, 12:10 PM
Hi Glen,

It is impossible to group, sort, filter, etc. on a column bound to ".", since this in not a filed on the object, but the object itself.

There is a much more natural and easier way to achieve your goal. You can add a new string property to your business object (or maybe the IDefaultModeOfContact interface) and call it, let's say "ContactDetails". In the getter of the property you can perform the business logic that you currently do in the IValueConverter. This will be much easier and the grid will be able to do everything out-of-the-box since this will be a normal data field like all the others. If you do not want to "touch" your original business object, you can implement the ICustomTypeDescriptor interface and add additional properties to your objects at runtime.

I hope this helps. Please, do not hesitate to contact us if you have any other questions.

Kind regards,
Ross
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
Glen
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Glen
Top achievements
Rank 1
Share this question
or