Editable GridViewComboBoxColumn with DropDown style

2 Answers 61 Views
GridView
Marian
Top achievements
Rank 2
Iron
Iron
Iron
Marian asked on 13 Jan 2025, 10:14 PM

Hello,

I would like to have combo box column in GridView editable by user, so user can select from dropdown list or type it manually. I have two possible scenarios, one with column mapped to other table, so it should be really only some value from dropdown, but I want to have it editable because of more values, and second with just string value, but I want to offer a dropdown for selecting most common values. It doesn't work in any case. Selecting from dropdown list is ok, but when I type an option manually, it reverts back to original value:

I have found very similar question here, but it's older and I don't know, how he solved it, it doesn't work for me, or more precisely, I don't know what's the difference, I don't see any important point I have missed.
https://www.telerik.com/forums/editable-gridviewcomboboxcolumn
https://docs.telerik.com/devtools/winforms/controls/gridview/features/editing/editors/how-to/allow-end-users-to-add-items-to-dropdownlisteditor

I have column defined like this:

dc = new DbDataContext();
var col = rgv.Columns["colProjID"] as GridViewComboBoxColumn;
col.DataSource = dc.Proj2s.OrderBy(o => o.Code);
col.FieldName = "ProjID";
col.DisplayMember = "Code";
col.ValueMember = "ID";
col.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDown;

If the solution isn't obvious, I can prepare test project, but I have it bound to database now, so it would take some time.

2 Answers, 1 is accepted

Sort by
0
Nadya | Tech Support Engineer
Telerik team
answered on 14 Jan 2025, 02:59 PM

Hello, Marian,

Thank you for writing.

Having a GridViewComboBoxColumn that allows editing and adding custom values to DropDownListEditor is a common scenario. The resources that you have found are useful on this topic. In brief, setting the DropDownStyle to DropDown allows typing into the text box field of DropDownListEditor. However, this is not enough to add the newly entered value into the items collection. This is because we restrict the editor to show only valid values. It is to the developer to consider how to handle the free user input. So, it is necessary to create a custom DropDownEditor to handle the user input according to your needs and add them as items into the drop down list. 

You should follow the approach described in this Allow end-users to add items to DropDownListEditor article. However, I noticed that the link at the end of the article is broken and this is why I am posting the correct link to our SDK project in GitHub here

You can also to the following KB which describes similar approach on this topic: Use GridComboBoxColumn with Custom Values - Telerik UI for WinForms - KB - Telerik UI for WinForms

I hope this resources will be helpful to achieve desired behavior. If you have any further questions do not hesitate to contact me.

Regards,
Nadya | Tech Support Engineer
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Marian
Top achievements
Rank 2
Iron
Iron
Iron
commented on 14 Jan 2025, 11:04 PM

Hello,

I have looked at it, I tried the second link, but result was even worse. Probably I am still missing something. I have problem with setting the value after manual edit, I don't want to add item to combo box by user. And in the scenario from my sample, where that combobox represents ID to subtable, I want to limit user to values from combobox. I would expect some overrides in properties setters, and I don't see anything special in those two samples, which would seem to me to solve the problem. I would expect, that after manual typing of value, default implementation should look to item collection and if it finds it, it will set it to bound datasource. There is also this sentence in the second link - "This is necessary because by default the cell is looking for the proper value in the editor data source". So, why it reverts it back? And when I type something not present in collection, result is empty cell. Maybe I should really prepare test project.

0
Marian
Top achievements
Rank 2
Iron
Iron
Iron
answered on 14 Jan 2025, 11:31 PM

Hello,

so I have prepared the test project. It's not fully equivalent, my test classes don't have INotifyPropertyChanged interface, also it doesn't check if ID is present in subtable, but I think it's sufficient for this question. Can you please look at it? Thanks.

Nadya | Tech Support Engineer
Telerik team
commented on 16 Jan 2025, 02:04 PM

Hello, Marian,

Thank you for providing a project.

RadDropDownList relies on the items from the date source. Free input text is not allowed to be saved and this behavior is by design. However, I have read carefully your requirements and it seems that you do not want to add new items to the combo box by the user. Am I right?

You said "I would expect, that after manual typing of value, default implementation should look to item collection and if it finds it, it will set it to bound datasource." This makes me thing that you are looking for the auto-complete function where RadDropDownListElement can accept some input, then search into the available items, and if some relevant result is found, it will suggest it to the user. Is this what you are looking for?

The RadDropDownList.AutoCompleteMode property controls auto-complete behavior and can be set to None, Suggest, Append and SuggestAppend. The following code snippet sets the AutoCompleteMode.SuggestAppend:

private void Rgv_CellEditorInitialized(object sender, GridViewCellEventArgs e)
{
    var editor = e.ActiveEditor as RadDropDownListEditor;
    if (editor != null)
    {
        (editor.EditorElement as RadDropDownListElement).AutoCompleteMode = AutoCompleteMode.SuggestAppend;
    }
}

Thus, when you type "P" in the text area it will suggest relevant items. In your scenario, you have P1, P2, and P3 which all start with the same letter, but this might be a simple data example. However, can you give this a try and let me know if this is desired behavior that you need to obtain or you need something more? 

Looking forward to your reply.

Tags
GridView
Asked by
Marian
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Nadya | Tech Support Engineer
Telerik team
Marian
Top achievements
Rank 2
Iron
Iron
Iron
Share this question
or