1
What is the right way to get the value of a ComboBox populated by a DataTable?
I’m using the following code:
private void ComboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
Consulta_cidade cidade = new Consulta_cidade();
var dataRowView = this.comboBox3.SelectedItem as DataRowView;
var valor = dataRowView.Row.ItemArray[1];
int a = Convert.ToInt32(valor);
comboBox2.DataSource = cidade.consulta_cidade(a);
comboBox2.ValueMember = "descricao";
comboBox2.DisplayMember = "descricao";
comboBox2.Update();
}
You can use the property
SelectedItemto take the display value and propertySelectedValueto take the code associated with the Display value.– gato
Dener, this method, cannot be applied here, because when we use a datatable to popular a combobox, the values return as object, not as string. That’s why I needed to use a Datarowview. Correct me if I’m wrong. Thank you
– Thomas Erich Pimentel
It’s WPF or Winforms?
– gato
Dener I’m working with Winforms
– Thomas Erich Pimentel
Where the value is returned as an object?
– gato
Just convert to whole:
int valor = Convert.ToInt32(comboBox3.SelectedValue);– gato
Dener, perfect, I redid the code as you said. and solved the problem. I do not know pq, yesterday was not getting. I could not identify the error. Thank you.
– Thomas Erich Pimentel
@Denercarvalho Post this as an answer.
– stderr
@zekk yes I will post
– gato