Combo or Dropdownlist display a Text according to its value

Asked

Viewed 379 times

-1

I do a search of my comic book and I bring in, say, number 237. This is the value of my Combo that is equivalent to the text Bradesco S/A. So I would like the Dopdownlist to display this text Bradesco S/A when I pass the value(237). I’m not getting it to work. Look what I’ve done.

for (int i = 0; i < cmbBancos.Items.Count - 1; i++)
                    {
                        if (cmbBancos.Items[i].Value == nm_banco)
                        {
                            cmbBancos_SelectedIndexChanged(this, EventArgs.Empty);
                            //cmbBancos.Text = cmbBancos.Items[i].Text.ToString();
                            break;
                        }
                    }

In the above way, it doesn’t work and not even with the commented code, either. Someone knows how to make it work?

Webform and C usage#.

  • Is this dropdown populated with bank numbers? Do you want the displayed value to be "Bradesco S/A" and the value to be 237? That’s it?

  • @pnet can put the code of how your combo is being loaded?

  • The combo is already loaded. All the banks that operate in Brazil are already in the combo and their respective numbers (value). When I type the Cpf or cnpj of a client, then the search goes in my system and picks up the bank, account and etc. of that client. Just need the combo to display this bank name passing a value to it. Everything is already registered, all the same. I just don’t know how to show in the combo automatically when value is passed.

1 answer

2


I believe this will suit you:

cmbBancos.SelectedValue = "237";

But this will only work if you have loaded the Dropdownlist by passing the Text and Value, example:

cmbBancos.Items.Add(New ListItem("Bradesco S/A", "237"))
  • The combo is already filled with all the banks operating in Brazil. I just want to display the text according to the value found in my BD.

  • @pnet If in your Dropdownlist you have not only entered the Text of the database, there is also the Value, so that I have put it will work!

Browser other questions tagged

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