Leave Selected specific item Combobox C# Winforms Visual Studio 2017

Asked

Viewed 1,374 times

1

I am developing an application that contains a customer register. In this register there is a ComboBox listing the cities in the database.

My question is this: How do I leave 'setate' the name of a city specifies?

For example: first city that lists by default is 'ABADIA DE GOIAS', but I would like the city that appears selected to be 'SAO PAULO', that is there through my list. How can I do that?

Code listing cities in Combobox:

String nomeConexao = LoginInfo.StringConexao;
String string_conn = ConfigurationManager.ConnectionStrings[nomeConexao].ConnectionString;
SqlConnection conn = new SqlConnection(string_conn);

String scom = "SELECT COD, CIDADE FROM CODMUNICIPIO ORDER BY CIDADE";

SqlDataAdapter da = new SqlDataAdapter(scom, conn);

DataTable dtResultado = new DataTable();
dtResultado.Clear();
CbCidade.DataSource = null;
da.Fill(dtResultado);

CbCidade.DataSource = dtResultado;
CbCidade.ValueMember = "COD";
CbCidade.DisplayMember = "CIDADE";
CbCidade.SelectedItem = "";
CbCidade.Refresh();
  • This is windowns Forms ?

  • That’s right Amadeu, I hadn’t specified, but I’ve corrected.

1 answer

2

You can set the property SelectedValue:

CbCidade.SelectedValue = 1; //supondo que 1 seja código de Sao Paulo

or you can set Text as well:

CbCidade.Text = "SAO PAULO";

Although I find it unnecessary to run a new query to search for the city code that should be parameterized, you should do so:

String sql2 = "SELECT COD FROM  CODMUNICIPIO WHERE CIDADE = '" + CidadeEmpresa + "'"; 

SqlCommand cmd2 = new SqlCommand(sql2, conn); 
SqlDataReader leitor = cmd2.ExecuteReader();
 if (leitor.HasRows)
 {
      leitor.Read();
      this.CbCidade.SelectedValue = leitor["COD"].ToString();
 }

The Correct, would you store the Company City Code in the variable CidadeEmpresa and not her name, so would just:

this.CbCidade.SelectedValue = CidadeEmpresa;

and if it is still not changing what is stored in the variable, just do so:

this.CbCidade.Text = CidadeEmpresa;
  • Ola Rovann, problem is that I have more than 5000 cities, and depending on the case will change the cities, The idea is that if the customer has the company in PORTO ALEGRE, at the time of starting a customer registration he already leave set PORTO ALEGRE to facilitate when registering, if it is NEW HEADQUARTERS leaves NEW HEADQUARTERS and so on.

  • You can select the item with linq in dtResultado and define in CbCidade.SelectedItem. Kind of: CbCidade.SelectedItem = dtResultado.SingleOrDefault(c => c.COD == codCidadeEmpresa);. So that you are storing in codCidadeEmpresa the city you want to suggest according to your criteria.

  • 1

    in this case would be a parameter of your system, which should be configured by your client, Diego gave an example of how to select this parameter, but to define the item selected in the combo (and that is what is in the question) is how I put

Browser other questions tagged

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