Reputation: 217
I'm working with windows form, in one form a list the data of a table and in the other one, I add the data. In the form that I list, I have the the form load to set the values of the table in a datagridview. So I want that when I click save in the saving form, I reload the other form where I list the data. I've tried something like:
form.refresh
but doesn't work. I tried closing the list form when clicking add, and then when I clicked save it would show up again, that worked, but is there other way I can do it?
Here is my code:
List form:
private void ListadoExpedientes_Load(object sender, EventArgs e)
{
dgvExpedientes.AutoGenerateColumns = false;
Exp = ExpedienteNG.GetExpedientes();
bExpedientes = new BindingList<Expediente>(Exp);
dgvExpedientes.DataSource = bExpedientes;
}
and here is the save form:
private void btnGuardar_Click(object sender, EventArgs e)
{
ListadoExpedientes listexp = new ListadoExpedientes();
listexp.Refresh();
}
Upvotes: 0
Views: 176
Reputation: 367
Class(Form) - Form2 (ex.) that holds the method that requires update in base form - Form1
In Form2 define constructor and a variable that holds a Form1 object
Form1 frm1;
public Form2(Form1 _frm1){
InitializeComponent();
...
this.frm1 = _frm1;
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.RefreshDatagrid);
}
In Form1
public void RefreshDataGrid()
{
dgvExpedientes.DataSource = null;
dgvExpedientes.DataSource = bExpedientes;
}
private void OpenForm2_Click(object sender, EventArgs e)
{
Form2 = new Form2 (this);
t.ShowDialog();
}
This code will update the dgvExpedientes in form1 , when you close the form2, so the event is triggered after you finish uploading records and close the form2. I hope this will help in your situation
Upvotes: 1
Reputation: 1
You can try to use Application.OpenForms collection to find your open forms. then call refresh from there. Example:
var listForm = Application.OpenForms.Cast<Form>().Where(x => x.Name == "ListadoExpedientes").FirstOrDefault();
if (listForm != null)
{
listForm.Refresh();
}
You might need to tweek some things, because I didn't test this.
Upvotes: 0