1
votes

I have one form with a DataGridView and combobox. Combobox is filled through DataSource in properties menu and I specify DisplayMember and ValueMember through this menu as well. 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.

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.

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. 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:

        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! :)

3
thanks for your help, but it didn't work for me.saba
For both link, it's not working?Prashant Pimpale
please take a look at:stackoverflow.com/a/31452304/7124761 if that not solved your answer then please post your Markup and CodeBehind code then will try on itPrashant Pimpale

3 Answers

2
votes

Update

In the main form that contain the 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.

1
votes

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 :)

0
votes

All I had to do was to fill up TableAdapter and then refresh combobox:

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