How to refresh DataSource of a ListBox

后端 未结 7 1230
轮回少年
轮回少年 2020-11-27 04:39

Form has one Combobox and one ListBox. When the \"Add\" button is clicked, I want to add the selected item from the ComboBox to the ListBox.

public partial c         


        
7条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-27 05:19

    The listbox didn't detect that you have changed the DataSource. It will only refresh when Datasource has changed, so set DataSource to null first:

    listBox1.DataSource = null;
    listBox1.DataSource = data;
    

    You could also clear the items then set the DataSource again:

    listBox1.Items.Clear();
    listBox1.DataSource = data;
    

提交回复
热议问题