簡體   English   中英

c# - 如何更新ComboBox

[英]c# - how can I update ComboBox

我有一個帶有DataGridViewcombobox表單。 Combobox通過屬性菜單中的DataSource填充,我也通過此菜單指定DisplayMember和ValueMember。 我有一個按鈕,當我點擊它時會顯示另一個表單,我可以在我的組合框的數據源中添加一個新項目。 當我關閉這個新表單時,我希望我的comobox的數據源刷新,我可以看到我剛剛在組合框中添加的新項目,但我不知道如何。

我努力了:

myComboBox.Refresh();

但什么都沒發生

我也試過這個:

myComboBox.Items.Add(myclass.myNewItem);

但它拋出一個異常:

設置datasource屬性時,無法修改items集合。

有人可以幫幫我嗎?

編輯:我想通了當我在第二個表單中添加一個新項目時一切都很好,新項目也添加到數據庫,但當我返回到第一個表單時聽起來像沒有發生的事情。 所以我將listBox添加到第二個表單中,並且在返回到第一個表單后我看到沒有添加任何內容。我真的不知道為什么即使我的數據庫發生了變化,組合框和列表框也會使用舊的數據源。 然后我嘗試了這個並且它有效:

在第二種形式中,我將我的新項目保存在一個類(名為transfer)中,當我返回到第一個表單時,執行了以下操作:

        DsMy.tblRow row = dsMy.tbl.NewtblRow();
        row.BeginEdit();
        row.Name = transfer.newName;
        row.Id = transfer.newId;
        row.EndEdit();

        dsMy.tbl.AddtblRow(row);


        this.Validate();
        tblTableAdapter.Update(dsMy.tbl);
        myComboBox.Refresh();

謝謝大家的幫助! :)

更新

在包含comboBox的主窗體中。 我想你的代碼就是這樣

private void btnAddNewObjectsButton_Click(object sender, EventArgs e)
        {
            AddNewObjectsForm form2 = new AddNewObjectsForm();
            form2.ShowDialog();
            if (form2.isSuccess)
            {
                this.myComboBox.DataSource = null;
                this.myComboBox.Items.Clear();
                this.myComboBox.DataSource = db.Object.ToList();//If you work with Entity frame work
                cmbCustomer.ValueMember = "Id";
                cmbCustomer.DisplayMember = "Name";
            }
        }

在另一種形式上,你的代碼將是這樣的

 public partial class AddNewdbObjects : Form
        {
         //isSuccess is a flage that will be true if the new object is added to db or no
        public isSuccess = false;
        //After Constructor in your click event
        private void btnSave_Click(object sender, EventArgs e)
                {
                    //Intialize data base source;
                    _db = new DBEntities();
                    dbObject obj = new dbObject();
                    obj.Name = txtName.Text;
                    try
                    {
                        _db.dbObject.Add(cust);
                        _db.SaveChanges();
                        isSuccess = true;
                        this.Close();
                    }
                    catch (Exception exc)
                    {
                        isSuccess = false;
                    }
        }
    }

這個解決方案應該適合你。

嘗試這個:

DataTable table = new DataTable();
DataRow row;
DataColumn column;         

// Create new DataColumn, set DataType, ColumnName and add to DataTable.    
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "ValueMember";
table.Columns.Add(column);

// Create second column.
column = new DataColumn();
column.DataType = Type.GetType("System.String");
column.ColumnName = "DisplayMember";
table.Columns.Add(column);

row = table.NewRow();
row["ValueMember"] = 1;
row["DisplayMember"] = "item";
table.Rows.Add(row);

comboBox1.DataSource = null;
comboBox1.DataSource = table;
comboBox1.DisplayMember = "DisplayMember";
comboBox1.ValueMember = "ValueMember";

我希望這可以幫助你 :)

我所要做的就是填充TableAdapter然后刷新組合框:

    tblTableAdapter.Fill(dsMy.tbl);
    myComboBox.Refresh();

暫無
暫無

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

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