简体   繁体   English

c# - 如何更新ComboBox

[英]c# - how can I update ComboBox

I have one form with a DataGridView and combobox . 我有一个带有DataGridViewcombobox表单。 Combobox is filled through DataSource in properties menu and I specify DisplayMember and ValueMember through this menu as well. Combobox通过属性菜单中的DataSource填充,我也通过此菜单指定DisplayMember和ValueMember。 I have a button that when I click on it another form will show and I can add a new item to my combobox's data source. 我有一个按钮,当我点击它时会显示另一个表单,我可以在我的组合框的数据源中添加一个新项目。 When I close this new form I want my comobox's datasource to refresh that I can see the new item that I just added in combobox, but I don't know how. 当我关闭这个新表单时,我希望我的comobox的数据源刷新,我可以看到我刚刚在组合框中添加的新项目,但我不知道如何。

I have tried: 我努力了:

myComboBox.Refresh();

but nothing happened 但什么都没发生

and I also tried this: 我也试过这个:

myComboBox.Items.Add(myclass.myNewItem);

but it throws an Exception: 但它抛出一个异常:

items collection cannot be modified when the datasource property is set. 设置datasource属性时,无法修改items集合。

Is anyone can help me, please? 有人可以帮帮我吗?

EDIT: I figured out that when I added a new item in second form everything is fine and new item is also add to database, but when I return to first form sounds like nothings happened. 编辑:我想通了当我在第二个表单中添加一个新项目时一切都很好,新项目也添加到数据库,但当我返回到第一个表单时听起来像没有发生的事情。 So I add listBox to second form and I saw nothing added after coming back to first form.I really don't know why combobox and listbox use old datasource even though my database changed. 所以我将listBox添加到第二个表单中,并且在返回到第一个表单后我看到没有添加任何内容。我真的不知道为什么即使我的数据库发生了变化,组合框和列表框也会使用旧的数据源。 then I tried this and it worked: 然后我尝试了这个并且它有效:

In the second form I saved my new item in a class(named transfer) and when I returned to first form did this: 在第二种形式中,我将我的新项目保存在一个类(名为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();

thanks everybody for your help! 谢谢大家的帮助! :) :)

Update 更新

In the main form that contain the comboBox. 在包含comboBox的主窗体中。 I guess your code like that 我想你的代码就是这样

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

on the other form your code will be like that 在另一种形式上,你的代码将是这样的

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

this solution should work with you. 这个解决方案应该适合你。

Try this: 尝试这个:

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";

I hope this helps you :) 我希望这可以帮助你 :)

All I had to do was to fill up TableAdapter and then refresh combobox: 我所要做的就是填充TableAdapter然后刷新组合框:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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