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

Grouping on Aggregate/ValueConverter Column

1 Answer 61 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Andy
Top achievements
Rank 1
Andy asked on 28 Nov 2011, 04:09 PM
I've spent enough time on this that it's probably best to ask for help. 

The SL DataGrid works great for us at this point with one exception. For one user group we needed to display a column based on some aggregate logic and displayed as a checkbox. This checkbox determines how they should treat the record, and it would be great to Group and/or Order on this column, but it shows a red 'X' when I try to drag the column to the grouping, and does nothing when I click to sort on it.

Here is the .xaml

<telerik:GridViewDataColumn 
DataMemberBinding="{Binding Converter={StaticResource HasProduct}}"
Header="Has Product?"
IsReadOnly="True"
IsGroupable="True">
<telerik:GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox 
IsEnabled="False"
IsChecked="{Binding Converter={StaticResource HasProduct}}"/>
</DataTemplate>
</telerik:GridViewColumn.CellTemplate>
</telerik:GridViewDataColumn>

I am sure that I have over-specified the data source, but that was introduced as part of frustrated troubleshooting.
The converter is defined as follows:

public class HasProduct :IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool HasProduct = false;
Order o = value as Order;
foreach (OrderDetail od in o.OrderDetails)
{
if (od.Category == "Product")
HasProduct = true;
}
return HasProduct;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    throw new NotImplementedException();
}

Any guidance on why this boolean field cannot be grouped/sorted and how best to fix that would be most welcome.
Thanks!

1 Answer, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 29 Nov 2011, 10:19 AM
Hi Andy,

You will need to "help" the grid by specifying the DataType of the column, since it cannot infer it from the converter. The converter can return anything, i.e. object and only you know that it returns booleans. The grid does not know that and due to this it cannot group.

But the better and more correct approach would be to move this business logic in your view model by introducing a new read-only boolean property called HasProduct and then bind a column to this boolean property. Then everything will work out-of-the-box. IValueConverter's are used to control the UI and are not the place to put business logic in. 

In other words, the thing that you are trying to do belongs in the view model (i.e. your business object class). That is the whole purpose of view models -- they adjust the model (DB) to fit in the view (UI).

I hope this helps.

Best wishes,
Ross
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
GridView
Asked by
Andy
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Share this question
or