ListBox
DataSource
UI development
coding
programming tips

How to refresh DataSource of a ListBox

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Refreshing a ListBox usually means making the UI reflect changes in the underlying collection. The correct approach depends on whether the control is bound to a live binding-aware collection or to a plain list that the UI cannot observe.

Understand What the ListBox Is Bound To

In Windows Forms, a ListBox can be bound directly to a collection through DataSource. If that collection is just a plain List<T>, the control has no built-in way to notice inserts, deletes, or edits after the initial binding.

That is why many developers try calling Refresh() and see no data change. Refresh() repaints the control; it does not rebuild the data binding. To update the displayed items, either use a collection type that notifies the UI or reset the binding explicitly.

Best Option: Use BindingList<T>

For WinForms, BindingList<T> is the simplest way to keep the ListBox synchronized with data changes.

csharp
1using System;
2using System.ComponentModel;
3using System.Windows.Forms;
4
5public partial class MainForm : Form
6{
7    private readonly BindingList<string> _items = new BindingList<string>();
8
9    public MainForm()
10    {
11        InitializeComponent();
12
13        listBox1.DataSource = _items;
14
15        _items.Add("Alpha");
16        _items.Add("Beta");
17    }
18
19    private void addButton_Click(object sender, EventArgs e)
20    {
21        _items.Add("Gamma");
22    }
23}

When the button adds a value, the ListBox updates automatically. No manual rebinding is needed because the collection itself notifies the binding layer.

If you display custom objects, set DisplayMember so the control knows what to show:

csharp
listBox1.DisplayMember = "Name";
listBox1.DataSource = peopleBindingList;

If You Must Use List<T>, Rebind Safely

Sometimes you already receive a plain List<T> from another API. In that case, the common pattern is to clear the binding and assign the data source again after updating the list.

csharp
1private List<string> _items = new List<string>();
2
3private void ReloadList()
4{
5    listBox1.DataSource = null;
6    listBox1.DataSource = _items;
7}
8
9private void addButton_Click(object sender, EventArgs e)
10{
11    _items.Add("New item");
12    ReloadList();
13}

This works, but it has tradeoffs. Rebinding can reset selection state, scroll position, and formatting. For a small static screen it may be acceptable; for a frequently changing UI, a binding-aware collection is the better design.

Use BindingSource for More Control

BindingSource sits between the control and the collection. It makes refresh behavior easier to manage and gives you a single place to reset the binding when needed.

csharp
1using System.Collections.Generic;
2using System.Windows.Forms;
3
4public partial class MainForm : Form
5{
6    private readonly BindingSource _bindingSource = new BindingSource();
7    private readonly List<string> _items = new List<string> { "One", "Two" };
8
9    public MainForm()
10    {
11        InitializeComponent();
12        _bindingSource.DataSource = _items;
13        listBox1.DataSource = _bindingSource;
14    }
15
16    private void renameButton_Click(object sender, EventArgs e)
17    {
18        _items[0] = "Updated";
19        _bindingSource.ResetBindings(false);
20    }
21}

ResetBindings(false) tells the UI to re-read the current values. This is especially useful when an object changes internally but the list itself has not gained or lost items.

Refreshing Item Properties

If the ListBox shows objects and you edit a property such as Name, the control still may not refresh unless the object supports change notification. For that, implement INotifyPropertyChanged.

csharp
1using System.ComponentModel;
2
3public class Person : INotifyPropertyChanged
4{
5    private string _name;
6
7    public string Name
8    {
9        get => _name;
10        set
11        {
12            if (_name == value) return;
13            _name = value;
14            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Name)));
15        }
16    }
17
18    public event PropertyChangedEventHandler PropertyChanged;
19}

With BindingList<Person> plus INotifyPropertyChanged, both collection changes and property edits can appear in the ListBox without tearing down the whole data source.

Common Pitfalls

Calling listBox1.Refresh() is the most common misunderstanding. It repaints pixels, but it does not force the control to requery a stale List<T>.

Another pitfall is rebinding on every small update. That works, but it can flicker and reset user interaction state. If the list changes often, move to BindingList<T> or BindingSource.

Developers also forget that object property changes need their own notification mechanism. Updating person.Name will not automatically redraw the item unless the object raises PropertyChanged.

Finally, make sure you update UI-bound collections on the UI thread. Cross-thread modifications can throw exceptions or create inconsistent rendering.

Summary

  • 'Refresh() repaints the control; it does not refresh stale bound data.'
  • 'BindingList<T> is the simplest way to get automatic item add and remove updates.'
  • For plain List<T>, reset DataSource or call BindingSource.ResetBindings.
  • Use INotifyPropertyChanged when displayed object properties can change.
  • Prefer binding-aware collections over repeated manual rebinding.

Course illustration
Course illustration

All Rights Reserved.