Unplanned
Last Updated: 22 Mar 2023 09:44 by ADMIN
Michael
Created on: 15 Mar 2023 11:57
Category: DropDownList
Type: Bug Report
2
RadDropDownList: Wrong item is selected and extra event is fired when removing item from the DataSource collection

Use the following code snippet and click the button to delete the first item from the BindingList:

        BindingList<DoctorTest> list = new BindingList<DoctorTest>();
        public RadForm1()
        {
            InitializeComponent();
            for (int i = 0; i < 5; i++)
            {
                list.Add(new DoctorTest(i,"Item"+i));
            }
            this.radDropDownList1.DisplayMember = "Name";
            this.radDropDownList1.ValueMember = "Id";
            this.radDropDownList1.DataSource = list;
            this.radDropDownList1.SelectedIndex = 4;
            this.radDropDownList1.SelectedValueChanged+=radDropDownList1_SelectedValueChanged; 
        } 

        private void radDropDownList1_SelectedValueChanged(object sender, EventArgs e)
        {
             RadMessageBox.Show(this.radDropDownList1.SelectedValue +" ---");
        }

        public class DoctorTest
        {
            public int Id { get; set; }
            public string Name { get; set; }

            public DoctorTest(int id, string name)
            {
                this.Id = id;
                this.Name = name;
            }
        }

        private void radButton1_Click(object sender, EventArgs e)
        {
            list.RemoveAt(0);
        }

 

3 comments
ADMIN
Dess | Tech Support Engineer, Principal
Posted on: 22 Mar 2023 09:44

Hello, Michael,

Indeed, when a new record is added to the collection, the selection is RadDropDownList is changed and it is navigated to the current position of the collection. This is expected behavior as it is explained in the referred forum thread.

Note that BindingList offers the ListChanged event where the  ListChangedEventArgs.ListChangedType indicates the operation - e.g. ItemDeleted when you remove a record from the list. However, it is fired after the SelectedValueChanged event of RadDropDownList. RadDropDownList offers the SelectedIndexChanging event which is fired before the item is removed. It is a cancelable event. Hence, you can detect the moment before and after removing the record and execute any action you need.

I believe that will be useful for your scenario.

Regards,
Dess | Tech Support Engineer, Principal
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.

Michael
Posted on: 15 Mar 2023 19:53

Hi,  Thank you for the suggestion bellow. The problem in that solution that binded list is updated somewhere else in the program. It is not a straight forward that I click a button that removes something from the list. So I am not able to wrap my code as you displayed.

I think this is also related to the issue I reported many years ago https://www.telerik.com/forums/adding-new-item-to-binding-list-causes-dropdownlist-to-select-it.

I did find a workaround back then by subscribing to bindingSource.ListChanged event which fires before item is added to the list. But when removing item from the list, this event is not triggered.

I think bottom line is, if we add/delete/modify any items in the binded list which is not selected, then SelectedValueChanged event should not fire, because we are not changing the selected value.  SelectedIndexChanging and SelectedIndexChanged events should be firing ONLY if item being added or removed is prior to the selected list, hence index of the selected item is changed.

I hope this gets fixed soon.

ADMIN
Dess | Tech Support Engineer, Principal
Posted on: 15 Mar 2023 12:07

Hi, Michael,

Thank you for reporting this undesired behavior.

The possible solution that I can suggest is to use BeginUpdate/EndUpdate block and unsubscribe from the event while removing the record:  

        private void radButton1_Click(object sender, EventArgs e)
        {
            this.radDropDownList1.SelectedValueChanged -= radDropDownList1_SelectedValueChanged;
            this.radDropDownList1.BeginUpdate();
            list.RemoveAt(0);
            this.radDropDownList1.EndUpdate();
            this.radDropDownList1.SelectedValueChanged += radDropDownList1_SelectedValueChanged;
        }

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.