Display Text for Invalid Values in GridView's Combobox Column
Environment
Product Version | Product | Author |
---|---|---|
2021.1.223 | RadGridView for WinForms | Desislava Yordanova |
Description
Consider the case where RadGridView contains a GridViewComboBoxColumn. The column's DataSource collections does not contain all of the values coming in the grid cells' values. This causes the cell to display empty text since none of the items in the GridViewComboBoxColumn.DataSource collection matches the cell's value. The following code snippet demonstrates a basic example for simplicity and better illustration:
Empty cell value for "Manager" value
It is expected behavior the cell to remain empty if its value is not present in the GridViewComboBoxColumn.DataSource collection. GridViewComboBoxColumn allows only valid values (according to predefined set of items in the DataSource) to be stored in the cells.
This article demonstrates how to handle such cases.
Solution
There are two general approaches that can be followed in this scenario:
1. Add the specific values (e.g. "Manager") to the GridViewComboBoxColumn.DataSource collection so they will be valid. For example, in the above code snippet, add "Manager" to the DataTable in the GetDataSource method. Thus, the GridViewComboBoxColumn.DataSource collection will contain all possible cells' values and the relevant text will be displayed:
2. Use a GridViewTextBoxColumn. Since the default editor for the GridViewTextBoxColumn is RadTextBoxEditor, it is necessary to be replaced with a RadDropDownListEditor in the EditorRequired event. It is necessary to specify the editor's DataSource, DisplayMember and ValueMember properties in a similar way like you do it for the GridViewComboBoxColumn.
Since the grid cells will display the ValueMember in this case, if the editor's DisplayMember and ValueMember are different, you can handle the CellFormatting event in this case and apply the desired display text to the CellElement.Text property.