Combobox is not being filled in by the Items I want

Asked

Viewed 138 times

7

I have a Combobox filled out from a list created with information obtained from a txt file. But instead of being filled in with the file information it is getting inside it -----> GeraRelatorio.Planta, would like to know why?

private void Form1_Load(object sender, EventArgs e)
{
    dateInicial.Value = DateTime.Today.AddDays(-1);
    dateFinal.Value = DateTime.Today.AddDays(-1);
    textBox1.MaxLength = 20;

    comboBanco.Items.Clear();
    List<Planta> plantas = new List<Planta>();

    using (StreamReader arquivo = File.OpenText(@"C:\Conexoes\Estados.txt"))
    {
        string linha;
        while ((linha = arquivo.ReadLine()) != null)
        {
            var espaçoArquivo = linha.Split(';');

            var planta = new Planta();
            planta.Local = espaçoArquivo[0];
            planta.Conexao = espaçoArquivo[1];

            plantas.Add(planta);
        }

    }

    foreach (Planta result in plantas)
    {
        comboBanco.Items.Add(result);
    }


}

private void comboBanco_SelectedIndexChanged(object sender, EventArgs e)
{
    comboBanco.SendToBack();
    FrmGrid formb = new FrmGrid();

    switch (((Planta)comboBanco.SelectedItem).Local)
    {
        case "CT":
            formb.lblLocal.Text = ((Planta)comboBanco.SelectedItem).Local;
            formb.lblConexao.Text = ((Planta)comboBanco.SelectedItem).Conexao;
            formb.Show();
            break;

        case "CU":
            formb.lblLocal.Text = ((Planta)comboBanco.SelectedItem).Local;
            formb.lblConexao.Text = ((Planta)comboBanco.SelectedItem).Conexao;
            formb.Show();
            break;

        case "AT":
            formb.lblLocal.Text = ((Planta)comboBanco.SelectedItem).Local;
            formb.lblConexao.Text = ((Planta)comboBanco.SelectedItem).Conexao;
            formb.Show();
            break;

        default:
            break;
    }
}

class Planta
{
    public string Local { get; set; }
    public string Conexao { get; set; }
}
  • 2

    Why are you adding to your item list comboBanco.Items.Add(result); instance of a plant! I think you should pass this list to your Datasource!

  • 1

    do as Virgilio said (although it makes no difference in the problem cited), enough comboBanco.DataSource = plantas; and remove that foreach...

2 answers

8


How are you filling your combobox with a list of an entity, we need to inform which field of that class represents the description that will be demonstrated and the value that that description represents.

Set a value (remembering that it must be some property of your class Planta) to the property DisplayMember of your combobox to present the text to the user:

comboBanco.DisplayMember = "Local";

Set a value (remembering that it must be some property of your class Planta) to the property ValueMember of your combobox to set the value for the selected item:

comboBanco.ValueMember = "Conexao";

2

Instead of adding a Planta in his ComboBox, add a SelectListItem.

foreach (Planta result in plantas)
{
    var item = new SelectListItem() { Text = result.Local, Value = result.Conexao }
    comboBanco.Items.Add(item);
}
  • works, but I disagree. If you need to take the "Plant" selected, it is simpler to do: Planta p = comboBanco.SelectedValue as Planta;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.