My grid looks like this:
<
telerikG:RadGridView
x:Name
=
"ExpositionFilterRules"
AutoGenerateColumns
=
"False"
SelectionMode
=
"Single"
FontSize
=
"14"
AllowDrop
=
"False"
CanUserDeleteRows
=
"False"
CanUserFreezeColumns
=
"False"
CanUserInsertRows
=
"False"
CanUserReorderColumns
=
"False"
ShowGroupPanel
=
"False"
RowIndicatorVisibility
=
"Collapsed"
ScrollMode
=
"Deferred"
IsEnabled
=
"True"
IsReadOnly
=
"False"
>
<
telerikG:RadGridView.Columns
>
<
telerikG:GridViewDataColumn
Header
=
"{StaticResource Promotions_ExpositionRule_ExpositionType}"
MinWidth
=
"200"
>
<
telerikG:GridViewDataColumn.CellTemplate
>
<
DataTemplate
>
<
ComboBox
ItemsSource
=
"{DynamicResource ExpositionTypes}"
SelectedItem
=
"{Binding ExpositionType}"
></
ComboBox
>
</
DataTemplate
>
</
telerikG:GridViewDataColumn.CellTemplate
>
</
telerikG:GridViewDataColumn
>
</
telerikG:RadGridView.Columns
>
</
telerikG:RadGridView
>
Grid shows objects with filled values (in this case object's property is ExpositionType). Everythig looks fine until grid is scrolled via vertical scrollbar. When row goes out of the visibility range null value is inserted into the object's ExpositionType property.
I dont use GridViewComboBoxColumn because I want to edit values without switching grid into edit mode (it requires more clicks).
My question is: how to prevent grid from deleting my data when scrolled?
5 Answers, 1 is accepted
I have tested your scenario, but I was not able to reproduce the issue you specify. What version of RadContols do you use ? Are all the Bindings well-defined and working ? What is the definition of your business objects for the ItemsSource of the grid and ComboBox ? May you try to use RadComboBox instead and test your scenario with it ?
Furthermore, if you want to skip the clicking on the GridViewComboBoxColumn for editing the item, you may set the property of the grid - EditTriggerst to "CellClick" and define the following style for the RadComboBox:
<
Window.Resources
>
<
StyleTargetType
=
"telerik:RadComboBox"
>
<
SetterProperty
=
"OpenDropDownOnFocus"
Value
=
"True"
/>
</
Style
>
</
Window.Resources
>
Kind regards,
Maya
the Telerik team
thanks for the answer. My Telerik's version is: 2010.3.1110.40
I prepared simple example: two classes and grid.
namespace WpfTestApplication
{
public class ExpositionType
{
protected string _name;
protected int _numerator;
public ExpositionType(string name)
{
this._name = name;
}
public override string ToString() { return this.Name; }
public virtual string Name
{
get { return _name; }
set { this._name = value; }
}
public virtual int Numerator
{
get { return _numerator; }
set { _numerator = value; }
}
}
public class Exposition
{
protected ExpositionType _expositionType;
protected decimal _netto;
public virtual ExpositionType Type
{
get { return this._expositionType; }
set { this._expositionType = value; }
}
public virtual decimal Netto
{
get { return _netto; }
set { this._netto = value; }
}
}
}
Xaml of main window:
<
Window
x:Class
=
"WpfTestApplication.MainWindow"
xmlns:telerik
=
"http://schemas.telerik.com/2008/xaml/presentation"
xmlns:telerikG
=
"clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"
xmlns:telerikD
=
"clr-namespace:Telerik.Windows.Data;assembly=Telerik.Windows.Data"
Title
=
"MainWindow"
Height
=
"200"
Width
=
"300"
>
<
Grid
>
<
telerikG:RadGridView
x:Name
=
"ExpositionRules"
AutoGenerateColumns
=
"False"
SelectionMode
=
"Single"
FontSize
=
"14"
AllowDrop
=
"False"
CanUserDeleteRows
=
"False"
CanUserFreezeColumns
=
"False"
CanUserInsertRows
=
"False"
CanUserReorderColumns
=
"False"
ShowGroupPanel
=
"False"
RowIndicatorVisibility
=
"Collapsed"
ScrollMode
=
"Deferred"
AutoExpandGroups
=
"True"
IsEnabled
=
"True"
IsReadOnly
=
"False"
>
<
telerikG:RadGridView.Columns
>
<
telerikG:GridViewDataColumn
Header
=
"Netto"
DataMemberBinding
=
"{Binding Netto}"
/>
<
telerikG:GridViewDataColumn
Header
=
"Type"
MinWidth
=
"200"
>
<
telerikG:GridViewDataColumn.CellTemplate
>
<
DataTemplate
>
<
ComboBox
ItemsSource
=
"{DynamicResource ExpositionTypes}"
SelectedItem
=
"{Binding Type}"
></
ComboBox
>
</
DataTemplate
>
</
telerikG:GridViewDataColumn.CellTemplate
>
</
telerikG:GridViewDataColumn
>
</
telerikG:RadGridView.Columns
>
</
telerikG:RadGridView
>
</
Grid
>
</
Window
>
and code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfTestApplication
{
public partial class MainWindow : Window
{
private List<
ExpositionType
> _expositionTypes;
private List<
Exposition
> _expositions;
public MainWindow()
{
InitializeComponent();
//
InitObjects();
this.Resources.Add("ExpositionTypes", this._expositionTypes);
this.ExpositionRules.ItemsSource = this._expositions;
}
private void InitObjects()
{
this._expositionTypes = new List<
ExpositionType
>();
//
this._expositionTypes.Add(new ExpositionType("Number1") { Numerator = 1 });
this._expositionTypes.Add(new ExpositionType("Number2") { Numerator = 2 });
this._expositionTypes.Add(new ExpositionType("Number3") { Numerator = 3 });
this._expositionTypes.Add(new ExpositionType("Number4") { Numerator = 4 });
this._expositionTypes.Add(new ExpositionType("Number5") { Numerator = 5 });
//
this._expositions = new List<
Exposition
>();
//
for (int i = 0; i < 20; i++)
{
this._expositions.Add(new Exposition() { Netto = i * 10 });
}
}
}
}
Start the application and select values in first three or more combo boxes. Next scroll the grid to bottom and release mouse button. Scroll grid to top and You'll see missing values.
May you try out the case when defining the ItemsSource of the ComboBox as a StaticResource ? I am sending you the sample project demonstrating this solution.
Let me know if it corresponds to your requirements.
Maya
the Telerik team
this solution works fine. Unfortunately I cannot use it. My code snippet is just an example. I need dynamic resource, because it may contain different list of objects.
Basically, on making a selection with the ComboBox, this selected value is saved in the corresponding property of the item. When you scroll up and down, the ComboBox relies on the Equals() method to match the value stored to a value in the itemssource. However, when you are using DynamicResources, the instances of those objects are different thought their properties are the same. As a result the Equals() method returns false and the values inside the ComboBox do not match resulting in losing selection.
You have a couple of options to overcome that issue. Firstly, you may introduce an integer type property in your Business Object that keeps the information about the selected value and define the ComboBox as follows:
<
ComboBox
ItemsSource
=
"{DynamicResource DynamicExpositionTypes}"
DisplayMemberPath
=
"Name"
SelectedValue
=
"{Binding SelectedExpositionType}"
SelectedValuePath
=
"Numerator"
/>
Here the SelectedExpositionType is of type int . Thus when scrolling even when a different instance comes the match will be correct
I am sending you a sample project illustrating the exact implementation.
Another approach may be to override Equals() method in the Business Object and compare the items according to the value of the "Numerator" property.
Best wishes,
Maya
the Telerik team