How to refresh DataSource of a ListBox
listbox1.DataSource
property looks for value changes but by assigning the same list all the time the value won't really change.
You can use a BindingList<T>
, instead of your List<T>
, to automatically recognize new items added. Your ShowData() method must be called once at startup.
public partial class MyForm:Form
{
public MyForm(){
InitializeComponent();
ShowData();
}
BindingList<MyData> data = new BindingList<MyData>();
private void ShowData()
{
listBox1.DataSource = data;
listBox1.DisplayMember = "Name";
listBox1.ValueMember = "Id";
}
private void buttonAddData_Click(object sender, EventArgs e)
{
var selection = (MyData)comboBox1.SelectedItem;
data.Add(selection);
}
}
ListBox has set datasource but Refresh does nothing
Refresh(): Forces the control to invalidate its client area and immediately
redraw itself and any child controls.
Refresh will not rebind your control, it will just cause the control to be redrawn. You will have to set the DataSource
again with
listBoxDays.DataSource = DBQuery.informationRetreval().DefaultView;
and re-bind it.
Source: Microsoft MSDN
Refresh a listbox's datasource
You will have to set the DataSource
of your listbox one more time after updating the source.
Something like below: It's my data:
public class Person
{
public int Age { get; set; }
public string Name { get; set; }
}
public class MyDataSource
{
public static List<Person> Persons = new List<Person>
{
new Person{Age=30,Name="Ram"},
new Person{Age=33,Name="Rahim"},
};
}
then in the form's constructor you can do:
listBox1.DataSource = MyDataSource.Persons;
listBox1.DisplayMember = "Age";
then for updation, something like below:
private void button1_Click(object sender, EventArgs e)
{
MyDataSource.Persons[0].Age = 45;
listBox1.DataSource = null;
listBox1.DataSource = MyDataSource.Persons;
listBox1.DisplayMember = "Age";
}
This is just an example change code according to your need.
How refresh items of a ListBox?
Just remove and add databinding again.
You can create method that can be used on first load and when new item was added:
void BindData()
{
listBox.DataSource = null;
listBox.DataSource = base_items;
listbox.DisplayMember = "Name";
}
So here is the code for adding new item and refreshing listbox:
base_items.Add(new Base_Item("Unnamed"));
BindData();
How to update a listbox that is bound to a datasource
Use the ResetBindings method to reread all the data from the BindingSource
and display the updated data in the control.
e.g.
BindingSource bs = new BindingSource(listboxchoices, null);
listbox1.DataSource = bs;
// make changes to listboxchoices
bs.ResetBindings(false);
ListBox not updated via DataSource
FWIW i finally found the issue and its that typical winforms threading thing:
putting the update of my listbox in a InvokeRequired block solves it:
public void UpdateData(Pair pair, double d)
{
Action action = () =>
{
var pd = pairList.First((x) => x.pair == pair);
pd.UpdateDiff(d);
};
if (listBox1.InvokeRequired)
{
listBox1.Invoke(action);
}
else
{
action();
}
}
How to refresh a listbox in C#?
You can either clear the whole listbox of its items and repopulate it from the database, or locate the item you're removing and remove it manually. Alternatively, using the "DataSource" property on the ListBox control and update the source whenever you run the query.
listbox Refresh() in c#
try the following
listBox1.DataBind()
How we can refresh items text in ListBox without reinserting it?
try
listBox1.Items[0] = listBox1.Items[0];
Related Topics
Convert Any Currency String to Double
How to Check If Two Expression<Func<T, Bool>> Are the Same
Differencebetween for and Foreach
How to Print the Contents of an Array Horizontally
Can a C# Lambda Expression Have More Than One Statement
Windows Forms Progressbar: Easiest Way to Start/Stop Marquee
Remove Text In-Between Delimiters in a String (Using a Regex)
Get a Generic Method Without Using Getmethods
Replacing a Char at a Given Index in String
Parsing a JSON File with .Net Core 3.0/System.Text.JSON
Get Ssid of the Wireless Network I am Connected to with C# .Net on Windows Vista
Connection String with Relative Path to the Database File
Winforms | C# | Autocomplete in the Middle of a Textbox
How Deterministic Is Floating Point Inaccuracy