RadDropDownList Rebind

11 posts, 1 answers
  1. Joseph
    Joseph avatar
    4 posts
    Member since:
    Aug 2010

    Posted 05 Oct 2010 Link to this post

    I'm trying to reload all the items in a RadDropDown box after something new gets added to the database. I have it set up right now that an event gets triggered when an item is added into the database, it calls the Rebind() method on the RadDropDownList, but nothing seems to happening. What should I do to get the RadDropDownList to reload?
  2. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 05 Oct 2010 Link to this post

    Hello Joseph,

    Are you sure the new element is added to the same instance of the collection that is set as a data source for the control?

    Best Regards,
    Emanuel Varga
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 05 Oct 2010 Link to this post

    Hi Joseph,

    Can you post your event call and ReBind method?
    Thanks
    Richard
  5. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 05 Oct 2010 Link to this post

    Hello again Joseph,

    I just wanted to ask you if you are not receiving data by any chance from a background thread, because if you are receiving data from a background thread you should use a synchronization event to avoid Cross-thread operations.

    Please check out this following example:
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Threading;
    using System.Windows.Forms;
    using Telerik.WinControls.UI;
     
    public partial class Form1 : Form
    {
        private SenderThreadEnum senderThreadType = SenderThreadEnum.Background;
     
        private RadDropDownList radDropDownList1 = new RadDropDownList();
     
        private SynchronizationContext synchronizationContext = SynchronizationContext.Current;
     
        public Form1()
        {
            InitializeComponent();
            this.Load += new System.EventHandler(Form1_Load);
        }
     
        void Form1_Load(object sender, System.EventArgs e)
        {
            radDropDownList1.Dock = DockStyle.Top;
            this.Size = new Size(200, radDropDownList1.Height + 63);
            this.Controls.Add(radDropDownList1);
     
            var button = new RadButton();
            button.Text = "Add Item...";
            button.Click += new System.EventHandler(button_Click);
            button.Dock = DockStyle.Bottom;
            this.Controls.Add(button);
     
            BindDropDown();
        }
     
        private void BindDropDown()
        {
            var list = new List<Test>();
            for (int i = 0; i < 10; i++)
            {
                list.Add(new Test(i, "test " + i));
            }
     
            // bind to a BindingList
            this.radDropDownList1.DataSource = new TestsCollection(10);
     
            //bind to a list
            //this.radDropDownList1.DataSource = list;
            this.radDropDownList1.DisplayMember = "Name";
        }
     
        void button_Click(object sender, System.EventArgs e)
        {
            if (senderThreadType == SenderThreadEnum.Main)
            {
                AddDataToList(new Test(radDropDownList1.Items.Count, "test " + radDropDownList1.Items.Count));
            }
            else
            {
                var thread = new Thread(AddDataFromADifferentThread);
                thread.IsBackground = true;
                thread.Start();
            }
        }
     
        private void AddDataFromADifferentThread()
        {
            synchronizationContext.Post((o) => AddDataToList(new Test(radDropDownList1.Items.Count, "test " + radDropDownList1.Items.Count)), null);
        }
     
        private void AddDataToList(Test item)
        {
            ((IList<Test>)radDropDownList1.DataSource).Add(item);
     
            if (!typeof(BindingList<Test>).IsAssignableFrom(radDropDownList1.DataSource.GetType()))
            {
                radDropDownList1.Rebind();
            }
        }
    }
     
    public enum SenderThreadEnum
    {
        Main,
        Background
    }
     
    public class Test
    {
        public int Id { get; set; }
     
        public string Name { get; set; }
     
        public Test(int id, string name)
        {
            this.Id = id;
            this.Name = name;
        }
    }
     
    public class TestsCollection : BindingList<Test>
    {
        public TestsCollection(int noItems)
        {
            for (int i = 0; i < noItems; i++)
            {
                this.Add(new Test(i, "test " + i));
            }
        }
    }

    Best Regards,
    Emanuel Varga
  6. Joseph
    Joseph avatar
    4 posts
    Member since:
    Aug 2010

    Posted 06 Oct 2010 Link to this post

    Everything happens on the same thread.

    This is the code on the Form that the User adds a new Item to the database
    From the AddSupport Form Class

    db = new Database(tableBox.SelectedItem.Text, supportTables[tableBox.SelectedItem.Text]);
    db.InsertSupport(valueBox.Text);
    db.closeConn();
      
    FinishUpdate(this);

    And this is the other ProjectForm which has the RadDropDownList

    AddSupport

     

    addSBU = new AddSupport("tblLU_Sbu", "SbuName");

     

    addSBU.FinishUpdate +=

    new AddSupport.FinishUpdating(finishUpdate);

     

    addSBU.Show();


    private void finishUpdate(object sender)
    {
        SbuBox.Rebind();
    }

     

     

  7. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 06 Oct 2010 Link to this post

    Hello Joseph,

    On each update you are creating a new instance or am I missing something?
    If yes, like my first answer, you no longer have the same instance in the data source of the dropdown so it cannot show the latest values.

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga
  8. Joseph
    Joseph avatar
    4 posts
    Member since:
    Aug 2010

    Posted 06 Oct 2010 Link to this post

    The only new Instances that get created is the AddSupport Form and the Database Class.

    The RadDropDownList and the DataSource instances doesn't change at all.
  9. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 06 Oct 2010 Link to this post

    Hi, 

    I'm not convinced (if my understanding of what you have posted is correct) that the new instance has an impact here. 
    Joseph, what's in the ReBind method? 
    Thanks
    Richard
  10. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 06 Oct 2010 Link to this post

    Hello,

    The Rebind() is from the RadDropDownList, method to call when you are not binding to a BindingList or anything that has an INotifyProperty changed event...

    In the test I've posted there are those two cases, the List and the BindingList, there should be no significant differences in behavior between binding to a List and a a DataTable or DataSet....

    Can you please set the data source to the DropDown again in the FinishUpdate method? (just to test if it works like this)

    Best Regards,
    Emanuel Varga
  11. Joseph
    Joseph avatar
    4 posts
    Member since:
    Aug 2010

    Posted 06 Oct 2010 Link to this post

    That worked thank you
  12. Kamal
    Kamal avatar
    1 posts
    Member since:
    Feb 2011

    Posted 16 Feb 2011 Link to this post

    This code is really very Helpful for me!

    Thank You!
Back to Top
UI for WinForms is Visual Studio 2017 Ready