I seem to be misunderstanding the ListBox.Refresh() method and I was hoping someone could help me out.
What I am trying to do:
I want to load a listbox's data (source = table of a sql database) upon initialization of a windows form. Also when the user adds data to the database I would like the listbox to update.
Logic:
I have a sql database as my source, it is set as:
listBoxDays.DataSource = DBQuery.informationRetreval().DefaultView;
DBquery.informationRetreval() is a static method within the my DBQuery static class. All it does is set up a table from the database and then returns the table.
I set the datasource within the same method as the initializeComponent (so my listbox will load with the existing data):
public Settings()
{
InitializeComponent();
listBoxDays.DataSource = DBQuery.informationRetreval().DefaultView;
}
When the user adds more data: I call a method in which I add the data to the database and then I call:
listBoxDays.Refresh(); //update listbox
The Problem: This does not update the listbox. Upon initialization of my listbox the data will populate but after it will not change (hence refresh does not work). Why is that? I could set the DataSource again but that feels sloppy. After looking into some of the documentation I noticed the event handler DataSourceChanged which maybe more for what I am looking for. Nevertheless why wouldn't refresh work?
Thank you for your patience. Please let me know if I need to be more clear.
Refresh will not rebind your control, it will just cause the control to be redrawn. You will have to set the
DataSource
again withand re-bind it.
Source: Microsoft MSDN
It'll only refresh when objects have changed (ex. a different query), not the data. You can do this:
to refresh it... everytime you need to.