简体   繁体   中英

Bind List<Double> to WinForms-Listbox

I have an small (probably dumb) issue with databinding. I try to bind a List

List<double> _measuredValues = new List<double>();

to a winforms ListBox.

In Form_Load I set:

lstMeasuredValues.DataSource = _measuredValues;

When I update the values, nothing appears?!

_measuredValues.Add(numBuffer);

One thing I thought about is a data type issue. But how do I change the type just to change it into a string?

lstMeasuredValues.DataSource = _measuredValues.ToString().ToList();

Another reason might be that the upper line of code is within another thread. But I think this should not be the problem.

How can I bind this list?

The better way is to clear the items and assign the DataSource again:

lstMeasuredValues.Items.Clear()// clear all items
lstMeasuredValues.DataSource = _measuredValues;

Or even you can define your own refresh function and call like the following:

public void RefreshList()
 {
   lstMeasuredValues.Items.Clear()// clear all items
   lstMeasuredValues.DataSource = _measuredValues;
 }

And call them when ever you need to refresh the list:

_measuredValues.Add(numBuffer);
RefreshList();
// Add more values 
RefreshList();

When I update the values, nothing appears?!

_measuredValues.Add(numBuffer);

In order to allow UI to reflect the data source modifications, the data source must provide some sort of a change notification. WinForms list data binding infrastructure uses ListChanged event of the IBindingList Interface . There is a standard provided BindingList<T> class which can be used instead of List<T> to get the desired behavior. All you need is changing this line

List<double> _measuredValues = new List<double>();

to

BindingList<double> _measuredValues = new BindingList<double>();

Another reason might be that the upper line of code is within another thread. But I think this should not be the problem.

That's not good. You must make sure you don't do that because ListChanged event is expected to be raised on the UI thread.

The problem is that the common List isn't the right choice for data binding. You should use BindingList if you want to keep updated the ListBox.

Try using it this way:

BindingList<double> bindList = new BindingList<double>(_measuredValues);
lstMeasuredValues.DataSource = bindList;

Keep in mind that when you add a new item in _measuredValues you have to manually refresh the binding, as far as I now, like this:

bindList.ResetBindings();

You could use a BindingList<double> as DataSource of your Listbox

List<double> _measuredValues = new List<double>();
BindingList<double> bindList = new BindingList<double>(_measuredValues);
lstMeasuredValues.DataSource = bindList;

Now everytime you need to add an element use the bindList variable and your listbox will update automatically as well as your _measuredValues list

One of the simplest way to do it is by putting:

lstMeasuredValues.DataSource = null; //the cheapest, trickiest, but the most important line
lstMeasuredValues.DataSource = _measuredValues;

Whenever your _measuredValues element is updated

您需要执行的所有操作以在更新后刷新列表:

lstMeasuredValues.Refresh();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM