簡體   English   中英

如何刷新列表框的數據源

[英]How to refresh DataSource of a ListBox

表單有一個組合框和一個列表框。 單擊“添加”按鈕時,我想將組合框中的所選項目添加到列表框。

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

在此示例中,所選項目將替換為 ListBox 中的新選擇。 我需要將該項目添加到列表中。

我的代碼有什么問題?

listbox1.DataSource屬性查找值更改,但通過始終分配相同的列表,值不會真正更改。

您可以使用BindingList<T>而不是List<T>來自動識別添加的新項目。 您的 ShowData() 方法必須在啟動時調用一次。

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);
    }
}

我建議使用BindingSource因為它會正確更新連接的控件。

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);
    }
}

更改控件數據源有時會產生奇怪的結果。

列表框沒有檢測到您更改了DataSource 它只會在Datasource發生變化時刷新,因此首先將DataSource設置為 null:

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

您還可以清除項目然后再次設置數據源:

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

或者,可能最正確的實現方法是使用提供的ObservableCollection<T> 它的唯一目的是實現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);
    }
}

因為ObservableCollection<T>實現了INotifyCollectionChanged ,所以只要您的數據發生變化,DataSource 綁定就會自動更新 ListBox。

當表單初始化時調用ShowData()在初始化時填充您的列表框

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

ShowData()方法,其中設置了BindingSourceDisplayMemberValueMember

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

在下面的實現中,當一個部門從數據庫中刪除時,列表框會刷新當前集合

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();
        }
    }

也許這個解決方案沒有更好的性能,但經過多次嘗試和幾個小時后,它對我有用:

這一行是在表單構造函數上執行的:

listBox1.DataSource = myData;

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

希望能幫助到你!

刷新也可以通過

listbox.ItemsSource = null;
listbox.ItemsSource = data;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM