开发者

Why does this work and this doesn't?

    public Form1()
    {
        InitializeComponent();
        comboBox1.DisplayMember = "Name";
        comboBox1.ValueMember = "ID";  
        LoadUsersToComboBox();
    }

    PersonRepository peopleRepo = new PersonRepository();

    private void LoadUsersToComboBox()
    {
        comboBox1.DataSource = peopleRepo.FindAllPeople().ToList();          
    }

    private void button2_Click(object sender, EventArgs e)
    {                        
        LoadUsersToComboBox();
    }

This method will load a comboBox with value only on the FIRST time, but not on subsequent attempts:

    private void LoadUsersToComboBox()
    {
        comboBox1.DataSource = peopleRepo.FindAllPeople(); /*Return IQueryable<Person>*/
    }

And this loads every time I call LoadUsersToComboBox():

    private void LoadUsersToComboBox()
    {
        comboBox1.DataSource = peopleRepo.FindAllPeople().ToList();
    }

Why does the first one only load the 开发者_JAVA百科first time?

Here is the code to the PeopleRepository class:

namespace SQLite_Testing_Grounds
{
    public class PersonRepository
    {
        private ScansEntities3 db = new ScansEntities3();

        public IQueryable<Person> FindAllPeople()
        {
            return db.People;
        }
    }
}


Solution is simple :

// This method returns the same reference every time
public IQueryable<Person> FindAllPeople()
{
    return db.People;
}

As a result :

// Nothing changes, DataSource old value is still the same (same reference,
// even is the content of the People list does change).
comboBox1.DataSource = peopleRepo.FindAllPeople();

// ToList() creates a new object each time, so DataSource is assigned to a 
// NEW object, and so calls a kind of invalidation of its visual.
comboBox1.DataSource = peopleRepo.FindAllPeople().ToList();

.
That's a data-binding basics, actually I'm not at all a WinForms guy (I know a lot more about WPF), but I think that internally, you have someting similar than :

private object dataSource;
public object DataSource {
    get {
        if (value != dataSource) {
            dataSource = value;
            RaisePropertyChanged("DataSource");
        }
    }
}


In this call, you are setting the datasource, but not the databind call to actually bind the data. Can you post the method where you call .databind?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜