1

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

stop-cran
  • 4,229
  • 2
  • 30
  • 47
Saba
  • 85
  • 10

3 Answers3

2

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.

Muhammad Hassan
  • 475
  • 2
  • 14
  • It's right but even I found the solution but still I haven't understand yet why in the first form my combobox still use old datasource though my database is upddated. – Saba Aug 15 '18 at 08:49
  • 1
    Take care if you are bound to a data source that does not implement the IBindingList interface, such as an ArrayList, the bound control's data will not be updated when the data source is updated. – Muhammad Hassan Aug 15 '18 at 09:45
  • I advice to review that link https://learn.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-bind-a-windows-forms-combobox-or-listbox-control-to-data – Muhammad Hassan Aug 15 '18 at 09:45
  • If this answer or any answer is correct, please mark it as correct answer. – Muhammad Hassan Aug 15 '18 at 14:16
1

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

Ehsan Mohammadi
  • 1,168
  • 1
  • 15
  • 21
0

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

    tblTableAdapter.Fill(dsMy.tbl);
    myComboBox.Refresh();
Saba
  • 85
  • 10