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; }
}
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!– novic
do as Virgilio said (although it makes no difference in the problem cited), enough
comboBanco.DataSource = plantas;
and remove that foreach...– Rovann Linhalis