New to Telerik UI for WinForms? Start a free 30-day trial
How to use RadDropDownList as editor in RadPropertyGrid
Updated over 1 year ago
Environment
| Product Version | Product | Author |
|---|---|---|
| 2019.1.219 | RadPropertyGrid for WinForms | Dimitar Karamfilov |
Description
You want to use RadDropDownList as editor for a specific property in RadPropertyGrid. In addition the a proper value should be displayed when the user is not editing as well. The below image shows how this will look when ready.

Solution
There a built-in editor that can be used for this PropertyGridDropDownListEditor but this editor is not used by for the standard types. First you need to use the EditorRequired event to change the editor. Then you can use the EditorInitialized event to set the data source. The final step is to format the when not in edit mode and the drop down list is not visible. The following snippet a complete example for this:
C#
public partial class RadForm1 : Telerik.WinControls.UI.RadForm
{
DataTable table = GetTable();
public RadForm1()
{
InitializeComponent();
new RadControlSpyForm().Show();
PropertyStoreItem intItem = new PropertyStoreItem(typeof(int), "Id", 1);
RadPropertyStore store = new RadPropertyStore();
store.Add(intItem);
this.radPropertyGrid1.SelectedObject = store;
radPropertyGrid1.EditorRequired += RadPropertyGrid1_EditorRequired;
radPropertyGrid1.EditorInitialized += RadPropertyGrid1_EditorInitialized;
radPropertyGrid1.ItemFormatting += RadPropertyGrid1_ItemFormatting;
}
private void RadPropertyGrid1_ItemFormatting(object sender, PropertyGridItemFormattingEventArgs e)
{
if (e.Item.Name == "Id")
{
var visualItem = e.VisualElement as PropertyGridItemElement;
var valueElememt = visualItem.ValueElement;
var value = (int)(visualItem.Data as PropertyGridItem).Value;
valueElememt.Text = GetValueById(value, "Id", "Name");
}
}
private string GetValueById(int value, string valueMember, string displayMember)
{
for (int i = 0; i < table.Rows.Count; i++)
{
var row = table.Rows[i];
if (value.Equals(row[valueMember]))
{
return row[displayMember].ToString();
}
}
return string.Empty;
}
private void RadPropertyGrid1_EditorInitialized(object sender, PropertyGridItemEditorInitializedEventArgs e)
{
if (e.Item.Name == "Id")
{
var editor = e.Editor as PropertyGridDropDownListEditor;
var element = editor.EditorElement as BaseDropDownListEditorElement;
element.DisplayMember = "Name";
element.ValueMember = "Id";
element.DataSource = GetTable();
}
}
private void RadPropertyGrid1_EditorRequired(object sender, PropertyGridEditorRequiredEventArgs e)
{
if (e.Item.Name == "Id")
{
e.EditorType = typeof(PropertyGridDropDownListEditor);
}
}
static DataTable GetTable()
{
DataTable table = new DataTable();
table.Columns.Add("Id", typeof(int));
table.Columns.Add("Name", typeof(string));
table.Rows.Add(1, "David");
table.Rows.Add(2, "Sam");
table.Rows.Add(3, "Christoff");
table.Rows.Add(4, "Janet");
table.Rows.Add(5, "Melanie");
return table;
}
}