Adding new item to binding list causes dropdownlist to select it

6 posts, 0 answers
  1. Michael
    Michael avatar
    41 posts
    Member since:
    Oct 2010

    Posted 30 Apr Link to this post

    Hello,

    I have a raddropdownlist which is binding to a BindingList.  When I add new item to the list, dropdown automatically selects this last added item.  How do I prevent it from selecting it?

    I tested with a combobox from .Net framework and it works fine.  Here is my code and I am also attaching my sample program.

    Please do not suggest to store the selection before I add item and then set it back afterwards.  My program is more complicated than this sample.  All my controls are binded and this BindedList get updated in different area of the program, so it is not simple for me to store and restore selected item.  I am sure it is just some property that need to be turned on or off in the control.

    You can download my sample program from here: https://www.dropbox.com/s/foc7i5xq6rfli9q/UserControlTests.zip?dl=0

     

    Here is my code

    public BindingList<string> ListItems { get; set; } = new BindingList<string> { "item1", "item2", "item3" };
    private int Count = 3;
    public Demo()
    {
    InitializeComponent();

    drpTest.DataSource = new BindingSource(this, "ListItems"); //raddropdownlist
    drpTest2.DataSource = new BindingSource(this, "ListItems"); //combobox
    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
    Count++;
    ListItems.Add("item" + Count);
    }

  2. Michael
    Michael avatar
    41 posts
    Member since:
    Oct 2010

    Posted 30 Apr Link to this post

    I was experimenting with some property.  The interesting thing, if you add Sorting, then after you add new item, the combobox gets cleared completely, so you loose selection.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 02 May Link to this post

    Hello Michael,

    Thank you for writing.

    By design, when the data layer in RadDropDownList changes the current position, this triggers the selection logic with the new position. In order to avoid it, you can cancel the RadDropDownList.SelectedIndexChanging event. It is appropriate to use a flag in order to store whether the selection change operation is allowed.
    bool flag = false;
     
    private void radButton1_Click(object sender, EventArgs e)
    {
        flag = true;
        items.Add(DateTime.Now.ToLongTimeString());
        flag = false;
    }
     
    private void radDropDownList1_SelectedIndexChanging(object sender,
        Telerik.WinControls.UI.Data.PositionChangingCancelEventArgs e)
    {
        e.Cancel = flag;
    }

    I hope this information helps. Should you have further questions I would be glad to help.

     Regards,
    Dess
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  5. Michael
    Michael avatar
    41 posts
    Member since:
    Oct 2010

    Posted 02 May in reply to Dess Link to this post

    Thank you Dess for your reply, but this is one solution I was not looking for.  When everything is as simple as it is in my sample program, this would be a great solution.  But I have several comboboxes on different forms that are binded to the same list.  List gets updated somewhere else in the program.  So I have to somehow pass this flag to each of my forms where the combobox is used.

    Are you sure there is not easier way to turn this feature off?  Maybe you should add a Boolean property like 'AutoSelectNewItem' to specify if combobox should automatically select the newely added item.

    Now, what about a problem with comobox is sorted?  When it is sorted and new item is being added, then selection gets cleared.  Are you suggesting to also use a flag and reselect the selected item before list was updated?

    Maybe you can suggest how to inherit from the raddropdownlist and implement this feature. The trick is, I have to know when list was updated through some event, without setting the flags in the click event.

  6. Michael
    Michael avatar
    41 posts
    Member since:
    Oct 2010

    Posted 02 May Link to this post

    My own question got me thinking ... I needed an event that fires before list changes and another event which fires after list changed.  I could not find any event in the dropdownlist which fires after SelectedIndexChanging when new item is added to the list. But, I found out that BindingSource object has an event called ListChanged which seems to be firing after list has changed and after SelectedIndexChanging was fired.  RadDropDownList has an event ItemDataBinding which seems to be firing before list changed.  I am not sure if these are the best events to use for this solution, but it seems to be working for now.  The only limitation, is that DataSource has to be set to BindingSource object and not to the list directly.

    So, below is my code that I've come up with.  Maybe someone can make a suggest if there are any potential issues that may come up and/or a better way of doing this to make this more universal for any object that is assigned to DataSource.

    public class RadDropDownListExtended : RadDropDownList
    {       
        private bool _DataBindingChanging;
             
        //need this method to ensure that I bind to BindingSource and not directly to the list
        public void SetDataSource(BindingSource bindingSource)
        {
            //remove list changed event in case binding changes
            if (base.DataSource != null && base.DataSource.GetType() == typeof(BindingSource))
                ((BindingSource)base.DataSource).ListChanged -= Bs_ListChanged;
                       
            base.DataSource = bindingSource;
     
            bindingSource.ListChanged += Bs_ListChanged;  //hook up to an event which fires after item was added to the list     
            ItemDataBinding += drpTest_ItemDataBinding;  // hook up to an even which seem to fires when list is about to change
            SelectedIndexChanging += drpTest_SelectedIndexChanging; //hook up to an event which will cancel the change of the selected item which should fire between other 2 events
        }
     
        private void Bs_ListChanged(object sender, ListChangedEventArgs e)
        {
            _DataBindingChanging = false;
        }
     
        private void drpTest_SelectedIndexChanging(object sender, Telerik.WinControls.UI.Data.PositionChangingCancelEventArgs e)
        {
            e.Cancel = _DataBindingChanging;
        }
     
        private void drpTest_ItemDataBinding(object sender, ListItemDataBindingEventArgs args)
        {
            _DataBindingChanging = true;
        }
     
    }

  7. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 04 May Link to this post

    Hello Michael,

    Thank you for writing back. 

    The provided solution for covering your case seems OK. If other customers have similar requests for adding a new property "AutoSelectNewItem" to control whether the newly added item will be selected, we will consider it in the future control's improvement.

    As to the sorting question, I confirm that the selection shouldn't be cleared. I have logged it in our feedback portal. You can track its progress, subscribe for status changes and add your vote/comment to it on the following link - feedback item.

    I have also updated your Telerik points.

    Currently, the possible solution that I can suggest is to store the selection before adding the new item and restore it after that: 

    int index = this.radDropDownList1.SelectedIndex;
    items.Add(DateTime.Now.ToLongTimeString());
    this.radDropDownList1.SelectedIndex = index;

    I hope this information helps. If you have any additional questions, please let me know.

    Regards,
    Dess
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Back to Top
UI for WinForms is Visual Studio 2017 Ready