Reputation: 483
First of all, I want to apologize for:
That said, let's go to the issue.
I have this window:
If I click the button marked in red:
This will open:
This is supposed to be a software for a market. The first window is responsible to order more thing to the inventory. The second window is responsible to add a supplier into the system.
The combobox shows all the suppliers on the system. I want when I finish adding a supplier on the second window after I clicked on the button highlighted with the red rectangle, the combobox will update automatically with the new data.
I used a "Update" button with this code:
this.tb_FornecedorTableAdapter.Fill(this.tccDataSet.tb_Fornecedor);
It worked, but I tried to use on FormClosing
, FormClosed
and Deactivate
events on the other windows and it didn't work at all (I modified the "this" on the code to a lot of this and it didn't help me). Is there a way to do what I want?
Upvotes: 4
Views: 1905
Reputation: 66
If the ComboBox is updated with the data from SQL Server then you can try this:
// When button Adicionar is clicked
private void buttonAdd_Click(object sender, EventArgs e)
{
using(Form formAdd = new Form()) // This is the Gerenciar Fornecedor form
{
formAdd.ShowDialog(this); // Show the form. The next statement will not be executed until formAdd is closed
// Put the your code to update the ComboBox items here
}
}
Upvotes: 2
Reputation: 594
In the first window declare a public methord:
public void RefreshCombo()
{
this.tb_FornecedorTableAdapter.Fill(this.tccDataSet.tb_Fornecedor);
}
Then in the first window add button click event
WindowB window=new WindowB(this);
WindowB.Show();
Then in the child window add a ctor method:
private WindowA windowParent;
public WindowB(WindowA parent)
{
InitializeComponent();
this.windowParent=parent;
}
In WindowB FormClosing Event
this.windowParent.RefreshCombo()
Upvotes: 2
Reputation: 43023
What you can do in this case is to add a property on the child form to store the combo box value and populate it when the combo box value changes. Also, create a method on the child form that will be called from the parent form. It will show the child form and return the combo box value.
public partial class ChildForm : Form
{
public ChildForm()
{
InitializeComponent();
}
private string _comboValue { get; set; }
public string ShowAndGetComboValue()
{
this.ShowDialog();
return _comboValue;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
_comboValue = comboBox1.SelectedItem.ToString();
}
}
On the parent form, you can then display the child form this way:
ChildForm form = new ChildForm();
string comboValue = form.ShowAndGetComboValue();
Upvotes: 1