Hi,
I want the user to be able to insert a new item in the combobox (via the underlying TextBoxEditor).
When entered, the user should see the newly added item as selected in the combobox.
I'm probably missing an event or not making the add/set DataSource at the good place.
I'm doing everything in the CellEndEdit event handler of the grid.
Reproduce: double-click on "Type 1", type any text and change the column.
If you re-open the content of the combo, the new item is there
public
partial
class
RadForm1 : Telerik.WinControls.UI.RadForm
{
private
List<KeyValuePair<
string
,
string
>> _list;
public
RadForm1()
{
InitializeComponent();
this
.Load += RadForm1_Load;
radGridView1.CellEndEdit += RadGridView1_CellEndEdit;
}
private
void
RadForm1_Load(
object
sender, EventArgs e)
{
_list =
new
List<KeyValuePair<
string
,
string
>>();
_list.Add(
new
KeyValuePair<
string
,
string
>(TestType.type_1.ToString(),
"Type 1"
));
_list.Add(
new
KeyValuePair<
string
,
string
>(TestType.type_2.ToString(),
"Type 2"
));
_list.Add(
new
KeyValuePair<
string
,
string
>(TestType.type_3.ToString(),
"Type 3"
));
SetColumnDataset();
LoadData();
}
private
void
SetColumnDataset()
{
var comboColumn = radGridView1.Columns[
"columnCombo"
]
as
Telerik.WinControls.UI.GridViewComboBoxColumn;
if
(comboColumn !=
null
)
{
comboColumn.DropDownStyle = RadDropDownStyle.DropDown;
comboColumn.DataSource = _list;
comboColumn.DisplayMember =
"Value"
;
// name to show
comboColumn.ValueMember =
"Key"
;
// name of element of Guid of specific service
}
}
private
void
LoadData()
{
radGridView1.Rows.Clear();
GridViewDataRowInfo rowInfo =
new
GridViewDataRowInfo(
this
.radGridView1.MasterView);
rowInfo.Cells[
"columnCombo"
].Value = Guid.NewGuid().ToString();
rowInfo.Cells[
"columnCombo"
].Value = TestType.type_3.ToString();
radGridView1.Rows.Add(rowInfo);
rowInfo =
new
GridViewDataRowInfo(
this
.radGridView1.MasterView);
rowInfo.Cells[
"columnCombo"
].Value = Guid.NewGuid().ToString();
rowInfo.Cells[
"columnCombo"
].Value = TestType.type_1.ToString();
radGridView1.Rows.Add(rowInfo);
}
void
RadGridView1_CellEndEdit(
object
sender, GridViewCellEventArgs e)
{
// e.ActiveEditor.GetType()
if
(e.Column == radGridView1.Columns[
"columnCombo"
])
{
if
(e.Value !=
null
&& e.Value.ToString() != String.Empty)
{
TestType enumVal;
bool
isEnum = Enum.TryParse<TestType>(e.Value.ToString(),
out
enumVal);
if
(isEnum)
{
// enumVal holds the value
}
else
{
AddNewColumnItem(Guid.NewGuid().ToString());
}
}
else
{
AddNewColumnItem(Guid.NewGuid().ToString());
}
}
}
private
void
AddNewColumnItem(
string
name)
{
if
(!_list.Any(x => x.Key == name))
{
_list.Add(
new
KeyValuePair<
string
,
string
>(name,
string
.Format(
"Type {0}"
, name)));
SetColumnDataset();
}
}
}
public
enum
TestType
{
type_1,
type_2,
type_3
}