There is no way of knowing what mistake you are making. The question does not show enough details for this.
So I’m going to show you a minimal and complete working example.
In the Load
of the Form I filled the first combobox and set the DisplayMember
and ValueMember
.
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.DataSource = new[]
{
new { Id = 1, Nome = "Item 0" },
new { Id = 1, Nome = "Item 1" }
}.ToArray();
comboBox1.DisplayMember = comboBox2.DisplayMember = "Nome";
comboBox1.ValueMember = comboBox2.ValueMember = "Id";
}
The SelectedIndexChanged
is practically the same, I just corrected the syntax error you have in the question (are two signs of equal, not one) and I took that class Item
because I don’t know how it was defined.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == 1)
{
comboBox2.Items.Add(new { Id = 1, Nome = "Item 2" });
}
}
See a working GIF
Thank you, it worked perfectly
– Cesar Goulart
@Cesargoulart great. For the record: you can mark the answer as correct using the V on her left side
– Jéf Bueno