How to load Combobox in C#?

Asked

Viewed 216 times

1

I have 2 Combobox: the 1st represents the provinces, the 2nd municipalities. When you click on Combobox 1 and select a province I want your municipalities to appear in Combobox 2.

  • is using Asp.net web.Forms or Asp.net mvc? The first thing you need to know is how to implement the event of selecting an item from the first combobox, and in this event fills the second combobox and so on. abs

  • 2

    Is this ASP.Net, Winforms or something? When asking a question, try to give as much information as possible, put the tags suitable, give a whirl in formatting so it makes it easy for everyone to understand the problem.

1 answer

1

using database, create a dropdown event in the combo that does the following::

//LIMPA OS ITENS DO COMBO
nomedoCombo.Items.Clear();

//CONECTA NO BANCO E RETORNA A CONEXÃO (DEVE TER SUA MANEIRA DE CONECTAR, ESSA É A MINHA)
ClassBancoDeDados Conn = new ClassBancoDeDados();


//PREPARA O SQL
String sSql = " SELECT campo FROM tabela order by campo";

MySqlDataReader DataReader = Conn.SqlQuery(sSql);
try
{
    if (DataReader.HasRows)
    {
       //se existem dados, serão adicionados no combobox
        while (DataReader.Read())
        {
            nomeDoCombo.Items.Add(DataReader.GetString(0));
        }
     }
}
catch
{
     return;
}

I like to put in the pq dropdown will always bring updated

until the next

Browser other questions tagged

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