Mutiselect drop down list column in RadGridView
Date Posted | Product | Author |
---|---|---|
Q3 2012 SP1 | Telerik UI for WinForms | Tsvetan Raikov |
How To
This article will demonstrate how to create a custom column in RadGridView with a multi-select drop down editor with check boxes. It allows keyboard selection with Ctrl and Shift keys and/or by using the mouse and the items' check boxes. The selection is stored in an integer array in the cell value.
As of Q3 2014 (version 2014.3.1021) Telerik UI for WinForms suite offers RadCheckedDropDownList control which combines RadDropDownList and RadAutoCompleteBox in order to provide functionality to check items in the drop down area and tokenize them in the text area. All previous functionality is preserved, such as visual formatting and data binding, which is now extended. RadCheckedDropDownList can also be used as an editor in RadGridView.
Solution
The solution to this case is to create a custom column which uses a specific cell and editor. The editor allows you to have multiple selection, while the cell holds the values. The column is used to easily incorporate the cell and the editor in RadGridView.
In the column we will define the desired DataType. In this case it will be an array of integers. Additionally, we will specify the desired editor and cell types:
Now, let's create the cells that we are going to use in our column. Here we will just override the SetContent method, where we will set the content of the cell - the selected item's text separated by semi column or empty string when the value is null:
Let's continue with the editor. First we have to override the CreateEditorElement method where we will return the CustomEditorElement which we will use. Also, we will have to override the Value property in the getter of which we will return the selected items as an array of integers and in the setter we will set the selected items according to the value:
Now, the editor element. It will consist of a LightVisualElement, which displays the values and is placed in the EditableArea of the control and a button for closing the drop down.
In the element's constructor we will first initialize the close button and add it accordingly to the sizing grip of the popup. We will also subscribe to its click, where we will close the popup. Next, we will set the SelectionMode to MultiSimple, which means that the users will be able to select item with mouse click or space button. We will also subscribe to the following events:
- PopupClosing - here we will cancel the popup closure when it contains mouse so we can use it to select items
- CreatingVisualItem - here replace the default visual item with a custom one
- ItemDataBinding - replace the default data item with a custom one
In the CreateChildElements override, we will initialize and add the LightVisualElement which will hold the text.
Another useful override is the one of the ShowPopup method, where prior calling the base functionality we will save the selected items and restore them after.
Finally, we will create a method that fires the OnTextChanged event, used to set the element's text accordingly:
In the custom data item will just add a property to store that information about a check operation:
And finally, the visual item. In the CreateChildElements override, we will initialize a StackLayoutElement, which will hold both the check box (RadCheckBoxElement) and the content element (a LightVisualElement). In the ToggleStateChanged event of the check box we will set the data item's Check property (which we have added in the CustomListDataItem class) and in the SynchronizeProperties override we will sync the check box and the text with its data item:
Here is how to put this column in action:
A complete solution in C# can be found here.