Update ComboBox

0

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";
    }
    
asked by Marcel Salazar 27.08.2018 в 21:39
source

2 answers

2

Check if you change your class in this way, I did the test and it worked, also check that you have elements in your list:

public void recargar_CBPersona()
{
    List<Persona> Personas = new List<Persona> {
        new Persona("Maria Martinez", "123456"),
        new Persona("Jose Franco", "123456"),
        new Persona("Alberto Urdaneta", "123456")
    };
    comboBox1.DataSource = Personas;
    comboBox1.DisplayMember = "Nombre";
    comboBox1.ValueMember = "DNI";
}

public class Persona
{
    public string Nombre { get; set; }
    public string DNI { get; set; }
    public Persona(string nombre, string dni)
    {
        Nombre = nombre;
        DNI = dni;
    }
}

Performing a test I got the following:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Gestion.Personas = new List<Persona>();
        Gestion.Personas.Add(new Persona("Maria Martinez", "123456"));
        Gestion.Personas.Add(new Persona("Jose Franco", "123456"));
        Gestion.Personas.Add(new Persona("Alberto Urdaneta", "123456"));
        recargar_CBPersona();
    }

    public void recargar_CBPersona()
    {
        CBPersonas.DataSource = Gestion.Personas;
        CBPersonas.DisplayMember = "Nombre";
        CBPersonas.ValueMember = "DNI";
    }

    public class Gestion
    {
        public static List<Persona> Personas { get; set; }
    }

    public class Persona
    {
        public string Nombre { get; set; }
        public string DNI { get; set; }
        public Persona(string nombre, string dni)
        {
            Nombre = nombre;
            DNI = dni;
        }
    }
}

    
answered by 27.08.2018 в 22:32
0

You have to control the result of the dialogue, just as you have recharged the combo at the same time you open the dialog, what you want is that when closing the dialog recharge the combo for that try the following:

In your dialogue in the save and close action you must indicate that the dialog will close with status OK

this.DialogResult = DialogResult.OK;
this.Close();

And when you open the dialog you will receive that parameter

IngresoPersona ingresopersona = new IngresoPersona();
if (DialogResult.OK == ingresopersona.ShowDialog(this))
{
    recargar_CBPersona();
}
    
answered by 27.08.2018 в 22:16