How to refresh DataSource of a ListBox

后端 未结 7 1168
轮回少年
轮回少年 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:06

    refreshing also works via

    listbox.ItemsSource = null;
    listbox.ItemsSource = data;
    
    0 讨论(0)
  • 2020-11-27 05:12

    Alternatively and probably the most correct way to implement this is to use the provided ObservableCollection<T>. It is designed with the sole purpose of implementing INotifyCollectionChanged.

    public partial class MyForm : Form
    {
        ObservableCollection<MyData> data = new ObservableCollection<MyData>();
    
        public MyForm()
        {
            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);
        }
    }
    

    Because ObservableCollection<T> implements INotifyCollectionChanged the DataSource binding will automatically update the ListBox whenever your data changes.

    0 讨论(0)
  • 2020-11-27 05:12

    Call ShowData() when the form initializes to populate your listbox on initialization

     public Department()
            {
                InitializeComponent();
                ShowData();
            }
    

    ShowData() Method, where BindingSource, DisplayMember and ValueMember are set

    private void ShowData()
                {
                    using (var uow = new UnitOfWork(new SellContext()))
                    {
                        listBox1.DataSource = uow.Departments.GetAll().ToList();
                        listBox1.DisplayMember = "DepartmentName";
                        listBox1.ValueMember = "DepartmentId"; 
                        //listBox1.Invalidate();       
                    }
                }
    

    In the implementation below when a department is deleted from database the listbox refreshes with the current collection

    private void button1_Click(object sender, EventArgs e)
        {
            try {
                using (var uow = new UnitOfWork(new SellContext()))
                {
                    int count = uow.Departments.FindDepartmentByName(txtDeptName.Text.ToString());
                    if (count>0)
                    {
                        MessageBox.Show("This Department Already Exists", "SellRight", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
    
                    else
                    {
                        department dept = new department();
                        dept.DepartmentName = txtDeptName.Text.ToString();
                        uow.Departments.Create(dept);
                        if (uow.Complete() > 0)
                        {           
                            MessageBox.Show("New Department Created", "SellRight", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            txtDeptName.Text = "";
                            ShowData();      
                        }
                        else
                        {
                            MessageBox.Show("Unable to add Department", "SellRight", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            txtDeptName.Text = "";
                            ShowData();
                        }
                    }
                }                              
            }
            catch(Exception ex)
            {
                ex.ToString();
            }
        }
    
    0 讨论(0)
  • 2020-11-27 05:17

    Maybe this solution has not the better performance, but after many tries and a few hours it was what it worked for me:

    This line was executed on the form constructor:

    listBox1.DataSource = myData;
    
    This lines were executed after the information was modified:
    listBox1.DataSource = new List<Movil>();
    listBox1.DataSource = myData;
    

    Hope it helps!

    0 讨论(0)
  • 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;
    
    0 讨论(0)
  • 2020-11-27 05:20

    I would suggest to use BindingSource as it would properly update connected controls.

    public partial class MyForm : Form
    {
        List<MyData> data = new List<MyData>();
        BindingSource bs = new BindingSource();
    
        public MyForm()
        {
            IntializeComponents();
            bs.DataSource = data;
    
           listBox1.DisplayMember = "Name";
           listBox1.ValueMember = "Id";
           listBox1.DataSource = bs;
        }
    
        private void buttonAddData_Click(object sender, EventArgs e)
        {
           var selection = (MyData)comboBox1.SelectedItem;
           data.Add(selection);
    
           bs.ResetBindings(false);
        }
    }
    

    Changing controls data source on fly produces strange result sometime.

    0 讨论(0)
提交回复
热议问题