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

Sorting and Filtering on Foreign key Columns

12 Answers 247 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Siva
Top achievements
Rank 1
Siva asked on 31 Aug 2009, 02:49 PM
Hi,

I have a grid with some foreign key columns in the datasource. I am converting those to Names by IValueConverter while binding the source. Now I need to do sorting and filtering with those columns. How can I do that. I read about IComparable Interface but where should I implement that?

Thanks,
Siva

12 Answers, 1 is accepted

Sort by
0
Stefan Dobrev
Telerik team
answered on 01 Sep 2009, 03:48 PM
Hello Siva,

You should implement IComparable on the type that is returned by the foreign key. Note that you should also implement equality operators (==) in order to enable distinct filtering on this type.

All the best,
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.
0
Siva
Top achievements
Rank 1
answered on 01 Sep 2009, 08:42 PM
Hi Stefan,

Thanks for your reply. I have implemented the IValueConverter interface as mentioned in msdn site(http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx) and binded as static resource to Grid Column.
I am just converting the ID to corresponding Name(string type) in my converter.
But when I try to drop the column header in filter area rad grid is throwing errors. Also no sorting working on this converted column. Can you provide code snippet to bind the foreign key converted values to grid also how to implement equality?

Thanks,
Siva
0
Kranthi
Top achievements
Rank 1
answered on 03 Sep 2009, 07:25 AM
hello telerik,

any updates on this, as i too have the same requirement. need to sort and group the foreign key columns in radgrid, if you can provide any sample code that would be greatly appreciated as i was working with implementing the IComparable and IEquatable but to no avail..

thanks in advance
Kranthi
0
Vlad
Telerik team
answered on 04 Sep 2009, 12:36 PM
Hi Kranthi,

Can you send us small project which demonstrates your case? I'll review your scenario and I'll gladly help you.

Greetings,
Vlad
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.
0
Siva
Top achievements
Rank 1
answered on 04 Sep 2009, 03:47 PM
Hi,
In my case I have implemented the IvalueConveter successfully as follows

    [ValueConversion(typeof(Int32), typeof(string))]
    public class UserNameConverter : IValueConverter
    { 
        string name;
        int id;
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return string.Empty;

            Int32.TryParse(value.ToString(), out id);
            name = GetUser(id).FirstName ?? string.Empty;
            return name;
        }
    }

Please tell me where should I implement IComparable and IEquatable interfaces?
should these also implemented to this converter or on My model layer object (User here).
If possible send me some sample code for this scenario.

Thaks,
Siva.


0
Vlad
Telerik team
answered on 07 Sep 2009, 05:16 AM
Hello Siva,

Sorting, filtering and grouping is not possible on IValueConverter - converters are for mostly for UI purposes. The grid will perform any data operation against the original data!

Greetings,
Vlad
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.
0
Siva
Top achievements
Rank 1
answered on 10 Sep 2009, 05:49 AM
Hi Telerik,

Thanks for your reply. Is there any other way to show foreign keys in the data as corresponding strings and also to sort on basis of the data displayed?
Suppose I am displaying User information in my Grid. User has a role and group assigned. so I have GroupID and RoleID in User Object. But I need to show those as Group Name and Role Name also should sort, filter and group by those. How can I do this? For displaying Names instead of ID's I used IvalueConverter. Please tell me how can I achieve all these requirements at once?

Thanks,
Siva.
0
Siva
Top achievements
Rank 1
answered on 13 Sep 2009, 12:40 PM
Hi,

This is a simple scenario, almost every one may get across. There should be some way to do this.
Some one can quickly tell me a solution??

Thanks,
Siva.
0
Milan
Telerik team
answered on 15 Sep 2009, 01:47 PM
Hello Siva,

Similarly to the case with IValueConverter, the grid cannot sort, filter, and group on data that is not available in the data that the grid is boud to. My suggestion is to add a property Group or GroupName to your User class or create a ViewModel if adding those properties to the Users class is not convenient.

All the best,
Milan
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.
0
mark baer
Top achievements
Rank 1
answered on 17 Feb 2011, 07:47 PM
What if you are using Related Fields/Columns.  For instance, my "Project" object has "CategoryId", but I am using the Entity Framework so the Project Object has a "Category" attached and the Category object has Name and Id.

In the Grid, I can add the Category Column and for the DataField, I have it ="Category.Name" and the data shows correctly.  But when I add that column to the Grouping, the grouping works, but I don't see the Category in the Header?  See html below.

Help?

<GroupByExpressions>
     <telerik:GridGroupByExpression>
          <GroupByFields>
               <telerik:GridGroupByField FieldAlias="Category" FieldName="Category.Name" SortOrder="Ascending">         </telerik:GridGroupByField>
          </GroupByFields>
     </telerik:GridGroupByExpression>
</GroupByExpressions>

BTW, I am using the latest download for demoing your controls.  If I can get Grouping/Filtering to work, my employer will probably pay for the upgrade.(We currently have your Silverlight controls).

Thanks
0
Milan
Telerik team
answered on 24 Feb 2011, 09:26 AM

Hi mark baer,

If Project object has Category object there is no problem to display or group by a property of Category.

<telerik:RadGridView Name="playersGrid" 
                             ItemsSource="{Binding Players}" 
                             AutoGenerateColumns="False">
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}"/>
        <telerik:GridViewDataColumn DataMemberBinding="{Binding Number}"/>
        <telerik:GridViewDataColumn DataMemberBinding="{Binding Position}"/>
        <telerik:GridViewDataColumn DataMemberBinding="{Binding Country}"/>
        <!-- Child property -->
        <telerik:GridViewDataColumn Header="Child property" DataMemberBinding="{Binding Club.Name}"/>
    </telerik:RadGridView.Columns>
</telerik:RadGridView>

I have also attached a sample application that demonstrated that. 


Best wishes,
Milan
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
mark baer
Top achievements
Rank 1
answered on 24 Feb 2011, 05:47 PM
Great, thanks.

mark
Tags
GridView
Asked by
Siva
Top achievements
Rank 1
Answers by
Stefan Dobrev
Telerik team
Siva
Top achievements
Rank 1
Kranthi
Top achievements
Rank 1
Vlad
Telerik team
Milan
Telerik team
mark baer
Top achievements
Rank 1
Share this question
or