I have a class with public static List<Persona> Personas { get; set; }
where people are saved.
then in the main form when loading
private void Principal_Load(object sender, EventArgs e)
{
recargar_CBPersonas();
}
public void recargar_CBPersonas()
{
CBPersonas.DisplayMember = "Nombre";
CBPersonas.ValueMember = "DNI";
CBPersonas.DataSource = Gestion.Personas;
}
and in the main form another form is opened where the data is filled
private void registrarToolStripMenuItem_Click(object sender, EventArgs e)
{
IngresoPersona ingresopersona = new IngresoPersona();
ingresopersona.ShowDialog();
recargar_CBPersona();
}
I had read on the internet that in this way the ComboBox was recharged but when closing the form where people register the ComboBox is still empty. I know that the data is being saved well because I have a function to search for CI and if it works correctly.
Observation *: If I remove these lines:
private void Principal_Load(object sender, EventArgs e)
{
recargar_CBPersonas();
}
And I add my first object to the list when I close the registration form, the combo is updated and the object comes out, but when the second object is added, it remains the same and only shows the first one.
Edito *: I solved it in this way, I do not know if it's the right one but for now I want it:
public void recargar_CBPersona()
{
comboBox1.Items.Clear();
foreach(Persona pers in Gestion.Personas)
{
comboBox1.Items.Add(pers);
}
comboBox1.DisplayMember = "Nombre";
comboBox1.ValueMember = "DNI";
}