I have a question regarding grouping of a GridViewDataColumn.
<Grid x:Name="LayoutRoot">
<Grid.Resources>
<local:EnumConverter x:Key="myConverter"></local:EnumConverter>
</Grid.Resources>
<telerik:RadGridView Name="gridView" >
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn Header="Custom Enum" DataMemberBinding="{Binding Gender}">
<telerik:GridViewDataColumn.CellTemplate>
<DataTemplate><TextBlock Text="{Binding Gender, Converter={StaticResource myConverter}}" /></DataTemplate>
</telerik:GridViewDataColumn.CellTemplate>
</telerik:GridViewDataColumn>
</telerik:RadGridView.Columns>
</telerik:RadGridView>
</Grid>
I explain my code : "Gender" property is Boolean type. If "Gender" = False , Converter return "male" and If "Gender" = True, Converter return "female" . It displays well and sorting well.
But when i grouping, panel group text display "false" or "true" . I want it displays "male" or "female" .
The same problem with filtering.
Thanks !
5 Answers, 1 is accepted
This is caused as a converter provides only UI visualization, but the grid performs the operations on the data-source level.
However, there are a couple of workarounds here. Firstly, you can create a ViewModel that embeds the the property which is converted - in your project Male/Female. Thus you will be able to bind the column directly to the property defined in this class. I am sending you a sample project illustrating that suggestion. In the application attached the data in the last column is a concatenation of the three before it.
Another possible approach would be to add new property in the class filling the grid. In its definition in the get-method you can return the result from the converter.
I hope that helps.
Maya
the Telerik team
Your sample project is very interesting.
But I haven't solved my problem. My project 's approach : MVVM and RIA Services. I created GenderEnum property in project silverlight (client) :
Partial Public Class Employee
...
Public ReadOnly Property GenderEnum() As String
Get
If Gender = 0 Then
Return EnumResource.Gender_Male
ElseIf Gender = 1 Then
Return EnumResource.Gender_Female
Return ""
End Get
End Property
Private Sub OnGenderChanging(ByVal value As Integer?)
If Me.EntityState <> System.ServiceModel.DomainServices.Client.EntityState.Detached Then
OnPropertyChanged(New PropertyChangedEventArgs("GenderEnum"))
End If
End Sub
...
End Class
After, I binding GridViewDataColumn with GenderEnum property. It shows in gridview well. But when I sort or group this column -> has an exception : "Load operation failed for query 'GetEmployee...'.No propery or field 'GenderEnum' exists in type Employee" .
So I have to use the converter and have problem like my first question. "Gender" is an example and my database have many fields like it.
Please help me.
Thanks.
In this case you can create partial class, expose a new property - Gender in your case and then bind it to the column you want.
For example:
namespace Converters_RIA.Web
{
public partial class Employee
{
public string Manager
{
get
{
if(this.ReportsTo == 2)
{
return "John";
}
else
{
return "Peter";
}
}
}
}
}
<
telerik:RadGridView
x:Name
=
"RadGridView1"
AutoGenerateColumns
=
"False"
ItemsSource
=
"{Binding Employees}"
>
<
telerik:RadGridView.Columns
>
<
telerik:GridViewDataColumn
DataMemberBinding
=
"{Binding Manager}"
/>
</
telerik:RadGridView.Columns
>
I am sending you a sample project illustrating the proposed solution.
Maya
the Telerik team
I'm sorry because that question is not clear.
So i created a sample project, download here .
I added a field Gender into table "Employee" in Northwind database.
My code :
Imports
TelerikGridView.My.Resources
Partial
Public
Class
Employees
Public
ReadOnly
Property
GenderEnum()
As
String
Get
Return
If
(Gender, EnumResource.Female, EnumResource.Male)
End
Get
End
Property
End
Class
<
telerikGridView:RadGridView
x:Name
=
"grdEmployee"
ShowGroupPanel
=
"True"
RowIndicatorVisibility
=
"Collapsed"
IsBusy
=
"{Binding IsLoading}"
AutoGenerateColumns
=
"False"
ItemsSource
=
"{Binding Data}"
SelectedItem
=
"{Binding SelectedItem, Mode= TwoWay}"
CurrentItem
=
"{Binding SelectedItem, Mode= TwoWay}"
ShowColumnFooters
=
"True"
IsReadOnly
=
"True"
>
<
telerikGridView:RadGridView.Columns
>
<
telerikGridView:GridViewDataColumn
DataMemberBinding
=
"{Binding FirstName}"
Header
=
"First name"
HeaderTextAlignment
=
"Center"
/>
<
telerikGridView:GridViewDataColumn
DataMemberBinding
=
"{Binding LastName}"
Header
=
"Last name"
HeaderTextAlignment
=
"Center"
/>
<
telerikGridView:GridViewDataColumn
DataMemberBinding
=
"{Binding BirthDate}"
Header
=
"Birth date"
HeaderTextAlignment
=
"Center"
DataFormatString
=
"{} {0:MM/dd/yyyy}"
TextAlignment
=
"Center"
>
</
telerikGridView:GridViewDataColumn
>
<
telerikGridView:GridViewDataColumn
DataMemberBinding
=
"{Binding GenderEnum}"
Header
=
"Gender"
HeaderTextAlignment
=
"Center"
>
</
telerikGridView:GridViewDataColumn
>
</
telerikGridView:RadGridView.Columns
>
</
telerikGridView:RadGridView
>
Partial
Public
Class
Home
Inherits
Page
Private
m_VMEmployee
As
New
VMEmployee
Public
Sub
New
()
InitializeComponent()
m_VMEmployee = TryCast(
Me
.DataContext, VMEmployee)
If
(m_VMEmployee IsNot
Nothing
)
Then
m_VMEmployee.MasterDataType = DataType.Employee
End
If
Me
.Title = ApplicationStrings.HomePageTitle
End
Sub
Private
Sub
Home_Loaded(
ByVal
sender
As
Object
,
ByVal
e
As
System.Windows.RoutedEventArgs)
Handles
Me
.Loaded
m_VMEmployee.LoadData()
End
Sub
End
Class
VMEmployee
? You can see it in my project. I use this way because i want to support multilanguage by using Resources.But Sorting, Grouping and Filtering Gender column -> work not well.
I hope you help me.
Thanks so much.
Firstly, let me clear that out. You want to change the field Gender depending on the language you are setting. So, for example, if the chosen language is Spanish, you require the value to be Mujer/Hombre. When you change it to French - Femme/Homme accordingly.
If that is the case, you can follow up the same idea as in the project I have attached previously. However, if your Property Gender is Female/Male, you can use the logic of changing the language as a boolean property and apply it as follows:
public string Gender
{
get
{
if(isSpanish)
{
return ES.Female;
}
else if(isFrench)
{
return FR.Female;
}
}
}
You can apply here whatever logic you need to implement.FR and ES are your custom Resource files for the different languages.
Best wishes,
Maya
the Telerik team