I am using a LINQ To SQL class, which is initialized before the constructor (the tables are added to it, it works):
DataClasses1DataContext dc = new DataClasses1DataContext(); Then I have a listbox:
listBox1.DataSource = dc.MyTable; listBox1.DisplayMember = "Name"; listBox1.ValueMember = "PersonID"; Then I want to add a new record:
MyTable mt = new MyTable(); mt.Name = "John Smith"; mt.PersonID = 30; dc.MyTable.InsertOnSubmit(mt); dc.SubmitChanges(); I want to refresh the listbox's datasource, by doing:
listBox1.DataSource = dc.MyTable; ...but the new record doesn't show up. However, if I update the datasource like this:
var q = from x in dc.MyTable select x; listBox1.DataSource = q; This works.
My only question is: why? I saw that some people set the DataSource to null, then back to the table, but that doesn't solve my problem.
(it's a winforms project)
listBox1.DataSource = dc.MyTable.ToList() seems to solve the problem, answered by Ehsan Sajjad in the comments.
listBox1.DataSource = dc.MyTable.ToList()?