To reproduce:
- add RadGridView and use the following code:
public Form1()
{
InitializeComponent();
List<ColorItem> list = new List<ColorItem>();
List<Color> colors = new List<Color>()
{
Color.Red,
Color.Black,
Color.Blue,
Color.Pink,
Color.Green,
Color.Yellow,
Color.Purple,
Color.Aqua,
Color.Orange,
Color.Fuchsia
};
for (int i = 0; i < 10; i++)
{
list.Add(new ColorItem(i, colors[i], colors[i].Name));
}
radGridView1.DataSource = list;
radGridView1.CellValidating += radGridView1_CellValidating;
}
private void radGridView1_CellValidating(object sender, CellValidatingEventArgs e)
{
if (e.ActiveEditor is GridColorPickerEditor && (Color)e.Value == Color.Aqua)
{
e.Cancel = true;
}
}
Steps:
1. Select the "Color" column of a certain cell and activate the editor;
2. Select Color.Aqua and press Tab to close the currently active GridColorPickerEditor;
As a result InvalidCastException is thrown.
Workaround: use a custom editor derivative of BaseGridEditor:
radGridView1.EditorRequired += radGridView1_EditorRequired;
private void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
{
if (e.EditorType == typeof(GridColorPickerEditor))
{
e.Editor = new CustomColorEditor();
}
}
public class CustomColorEditor : BaseGridEditor
{
TypeConverter converter = TypeDescriptor.GetConverter(typeof(Color));
protected override Telerik.WinControls.RadElement CreateEditorElement()
{
return new GridColorPickerElement();
}
public override object Value
{
get
{
GridColorPickerElement editorElement = this.EditorElement as GridColorPickerElement;
return editorElement.GetColorValue();
}
set
{
GridColorPickerElement editorElement = this.EditorElement as GridColorPickerElement;
if (value is Color)
{
editorElement.SetColorValue((Color)value);
}
else if (value is string)
{
editorElement.SetColorValue((Color)converter.ConvertFromString((string)value));
}
}
}
}