I am trying to update the data of a DataGridView when I press a button from another. To explain myself better, I have a main form where all registered partners are displayed, then a secondary form from where I create new partners. I need that when I press the button that saves the new partner, I update the DataGridView of the main form. I have tried to achieve this with an interface that communicates to the 2 forms and calls the update method but it does nothing. As a clarification, I am not looking to pass data from one form to another, I am looking to be able to call the method that is responsible for loading the grid but from another child form.
The main form code:
public partial class frm_principal_socio : Form, IForm//implementa la interfaz que cree para llamar al metodo de actualizacion
{
private void frm_principal_Load(object sender, EventArgs e)
{
this.llenarCombos();
this.cargarGrillaInscripciones(svInscripcion.getInscripciones());
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
timer1.Start();
}
//metodo para llenar el DataGridView
public void cargarGrilla(IList<DTO_Socio> lista)
{
dgv_socios.Rows.Clear();
if (lista != null && lista.Count > 0)
{
habilitarBotonesNoError();
foreach (DTO_Socio dto in lista)
{
dgv_socios.Rows.Add(new Object[]
{
dto.NumeroSocio,
dto.Nombre,
dto.Apellido,
dto.Dni
});
}
}
}
//metodo que actualiza la grilla desde la interfaz
public void actualizarDatos()
{
this.cargarGrillaInscripciones(svInscripcion.getInscripciones());
}
}
interface code
public interface IForm
{
void actualizarDatos();
}
Code of the secondary form, this is the form that calls the main one:
private void btn_guardar_Click(object sender, EventArgs e)
{
//......aca esta el codigo que valida los textBox y los datos ingresados
//...
retorno = (service.registrarSocio(dto) == 1);//valida que en la base de datos se haya creado el socio nuevo y si es correcto retorna 1
if (retorno == true)
{
//bloque de código que debería actualizar el otro formulario
IForm frm_p = this.Owner as IForm;
if (frm_p != null)
{
frm_p.actualizarDatos();//llamo al actualizar datos del principal
}
MessageBox.Show("Socio registrado con exito", "Exito", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.limpiarCampos();
}
else
{
MessageBox.Show("Hubo un problema al registrar al socio, intente nuevamente", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.limpiarCampos();
}
}
I add the code of the button that calls the form to register a new user:
private void btn_socios_Click(object sender, EventArgs e)
{
Form frm = Application.OpenForms.Cast<Form>().FirstOrDefault(x => x is frm_nuevo_socio);
if (frm == null || frm.IsDisposed == true)
{
frm = new frm_nuevo_socio();
frm.ShowDialog();
}
else
{
frm.BringToFront();
}
}
To clarify, the method that fills the DataGridView works perfectly, it works at startup or when applying a filter to it on the main form. The new member form also works perfectly since it registers the member in the database, the only problem is that I cannot call the method to update the DataGridView from the new member form and thus show that the new member has been added.
I've been doing some research and this code should work but it doesn't do anything, when I go to the main form there are the same partners as before I created the new one. Another solution is to call the button that refreshes the DataGridView, but it doesn't either or I'm possibly doing it wrong.
Franco, now that the doubts are clarified, I tell you that the code does not work for you because you are not passing the reference of
Owner
when you open the formfrm_nuevo_socio
.The solution is to modify the method invocation
ShowDialog()