This is a migrated thread and some comments may be shown as answers.

RadDropDownList Rebind

10 Answers 313 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Joseph
Top achievements
Rank 1
Joseph asked on 05 Oct 2010, 06:06 PM
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?

10 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 05 Oct 2010, 07:08 PM
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
0
Richard Slade
Top achievements
Rank 2
answered on 05 Oct 2010, 10:25 PM
Hi Joseph,

Can you post your event call and ReBind method?
Thanks
Richard
0
Emanuel Varga
Top achievements
Rank 1
answered on 05 Oct 2010, 11:09 PM
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
0
Joseph
Top achievements
Rank 1
answered on 06 Oct 2010, 03:25 PM

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();
}

 

 

0
Emanuel Varga
Top achievements
Rank 1
answered on 06 Oct 2010, 03:31 PM
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
0
Joseph
Top achievements
Rank 1
answered on 06 Oct 2010, 03:47 PM
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.
0
Richard Slade
Top achievements
Rank 2
answered on 06 Oct 2010, 03:49 PM
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
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 06 Oct 2010, 03:54 PM
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
0
Joseph
Top achievements
Rank 1
answered on 06 Oct 2010, 04:08 PM
That worked thank you
0
Kamal
Top achievements
Rank 1
answered on 16 Feb 2011, 08:10 AM
This code is really very Helpful for me!

Thank You!
Tags
DropDownList
Asked by
Joseph
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Richard Slade
Top achievements
Rank 2
Joseph
Top achievements
Rank 1
Kamal
Top achievements
Rank 1
Share this question
or