Home / Community & Support / Knowledge Base / RadControls for WinForms / GridView / Create editable RadComboBoxEditor

Create editable RadComboBoxEditor

Article Info

Rating: Not rated

Article information

Article relates to

RadControls for WinForms, Q2 2010 SP2

Created by

 Nikolay Diyanov

Last modified

 Sept 20, 2010

Last modified by

 Alexander Georgiev, Telerik



INTRODUCTION

By default, the GridViewComboBoxColumn opens a RadDropDownListEditor which allows you to select a value, but the editor itself remains read-only. The purpose of this article is to demonstrate how you can add and remove items to the RadDropDownListEditor.

SOLUTION

You can find solutions in C# and VB.NET at the bottom of this article.



The current example demonstrates a custom editor which is bound to a collection of strings. The user can add strings/items to the custom editor by typing the value and then pressing the Enter key. The user can remove strings/items as well by a RadButtonElement which is assigned to each RadListVisualItem.

An example of implementing a custom editor is demonstrated in our help documentation. In our case, we will inherit from the RadDropDownListEditor class:

public class MyComboBoxEditor : RadDropDownListEditor
{
    protected override Telerik.WinControls.RadElement CreateEditorElement()
    {
        return new MyComboBoxEditorElement();
    }
}

As you can see in the code snippet above, we return an instance of a custom MyComboBoxEditorElement which descends from RadDropDownListElement. This element is the actual combobox which you see when you open the combo editor.

There are several key parts in the MyComboBoxEditorElement:

1. Adding Remove buttons to the RadListVisualItems.
This part adds Remove buttons to the items when the MyComboBoxEditorElement is loaded:

private void ListElement_CreatingVisualItem(object sender, CreatingVisualListItemEventArgs args)
{
    // add button element
    RadListVisualItem visualItem = new RadListVisualItem();
    visualItem.Padding = new Padding(12, 0, 0, 0);
 
    RadButtonElement button = new RadButtonElement();
    button.MinSize = new Size(12, 12);
    button.MaxSize = button.MinSize;
    button.Text = "x";
    button.TextAlignment = ContentAlignment.MiddleLeft;
    button.Click += new EventHandler(button_Click);
 
    visualItem.Children.Add(button);
    args.VisualItem = visualItem;
}

 In the Click event of the RadButtonElement, we get the Data item from the respective RadListVisualItem and then remove it from the DataSource of the GridViewComboBoxColumn:

private void button_Click(object sender, EventArgs e)
{
    // Remove the item from the DataSource collection
    RadListVisualItem item = (RadListVisualItem)((RadButtonElement)sender).Parent;
    ((BindingList<string>)this.DataSource).Remove(item.Data.ToString());
    this.SelectedIndex = -1;
}

2. Checking if a typed value exists in the DataSource.
This part prevents a new string from entering in the DataSource if the same string already exists there:

private bool ExistsInDataSource(string currentText, BindingList<string> dataSource)
{
    for (int i = 0; i < dataSource.Count; i++)
    {
        if (currentText == dataSource[i])
        {
            return true;
        }
    }
  
    return false;
}

3. Overriding the ProcessKeyDown method:
When the user presses the Enter key after a value is typed in the combo editor, we check if the item exists in the current DataSource collection. If it does not exist, we add it to the DataSource collection.

protected override void ProcessKeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        BindingList<string> stringsDataSource = (BindingList<string>)this.DataSource;
        string currentText = this.Text;
 
        if (!ExistsInDataSource(currentText, stringsDataSource))
        {
            stringsDataSource.Add(this.Text);
            this.SelectedIndex = this.Items.Count - 1;
            return;
        }
    }
    base.ProcessKeyDown(sender, e);
}

Last, but not least, here is how we setup our GridViewComboBoxColumn:

GridViewComboBoxColumn comboCol = new GridViewComboBoxColumn();
comboCol.Name = "ComboColumn";
comboCol.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDown;
comboCol.Width = 100;
this.radGridView1.Columns.Add(comboCol);
BindingList<string> list = new BindingList<string>();
list.Add("1");
list.Add("2");
list.Add("3");
comboCol.DataSource = list;

Comments

There are no comments yet.
If you'd like to comment on this KB article, please, send us a Support Ticket.
Thank you!

Please Sign In to rate this article.