RadGridView - filter columns with a converter

1 Answer 175 Views
GridView
Daniel
Top achievements
Rank 2
Iron
Iron
Daniel asked on 29 Nov 2022, 01:21 PM

Hi Telerik support team,

I’m strugling with filtering a special case in the wpf gridview. Let me explain..

 I’ve a mainViewModel with an ObservebleCollection of type SubViewModel. Each subViewModel is a row in the grid. In the subViewModel I’ve a float property which is bound to a column through a valueConverter. The float (a representation of millimeters in the metric system) gets converted to inches in the imperial system. Inches can contain characters which are not supported by float (like /, - etc.) That’s why I use a converter to change it into a string. The converter works correct, also the convert back. I get a correct float value setted in my subViewModel.

The issue I walk into is when I filter the column. The filter mechanism seems to set a value in a fieldFilterControlViewModel and I get an error when I filter.

Can I change this behaviour? I would be nice that the filtervalue gets through the convertBack first and then apply the filter. So a converter on the filter inputvalue. Or, filtering directly on the showed values in the grid would also be ok.

Please help me out of this.

Regards,

Daniel

Error:

System.Windows.Data Error: 8 : Cannot save value from target back to source. BindingExpression:Path=Value; DataItem='FieldFilterControlViewModel' (HashCode=31636323); target element is 'TextBox' (Name=''); target property is 'Text' (type 'String') FormatException:'System.FormatException: Input value was not in a correct format.

   at Telerik.Windows.Controls.GridView.FieldFilterControlViewModel.set_Value(Object value)'

1 Answer, 1 is accepted

Sort by
0
Accepted
Martin Ivanov
Telerik team
answered on 01 Dec 2022, 11:01 AM

Hello Daniel,

The DataMemberBinding of the GridViewColumn doesn't support IValueConverter usage. You can use a converter to change the displayed value, but the underlying data operations like filtering, grouping and sorting won't work with the converted value. The recommended approach for this scenario is to define an additional property in the row's data model that converts the original property value and use the additional property in the DataMemberBinding. For example:

public class SubViewModel : ViewModelBase
{
	private const float inchPerMm = 0.039f;
	private float millimeters;
	private string inchesString;

	public float Millimeters
	{
		get { return millimeters; }
		set 
		{ 
			millimeters = value;
			OnPropertyChanged(nameof(Millimeters));
			this.InchesString = (millimeters * inchPerMm).ToString();
		}
	}

	public string InchesString
	{
		get { return inchesString; }
		set
		{
			inchesString = value;
			OnPropertyChanged(nameof(InchesString));
		}
	}
}

 <telerik:GridViewDataColumn DataMemberBinding="{Binding InchesString}" />

I hope that helps.

Regards,
Martin Ivanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
GridView
Asked by
Daniel
Top achievements
Rank 2
Iron
Iron
Answers by
Martin Ivanov
Telerik team
Share this question
or