C# How to get combobox value?

Asked

Viewed 49 times

1

Hello, my friends!

I am programming in C# and use Visual Studio 2019 to create an application in Forms. I have a combobox that I linked to a table using the Datasource property. When the user selects a value, I need two variables to receive Displaymember and Valuemember values. The routine I did is as follows:

private void Cb_Campo_SelectionChangeCommitted(object sender, EventArgs e)
{
    if (_Modo != 0)
    {
        Cb_Campo.DisplayMember = CampoTexto;
        Cb_Campo.ValueMember = CampoChave;
        ValorChave = int.Parse(Cb_Campo.SelectedValue.ToString());
        ValorTexto = Cb_Campo.SelectedItem.ToString();
    }
}

By default, before the selection, the field displays the "Select" text. With the above code, when the user selects a value from the list, the variable Key Value receives the correct value, but the variable Text Value receives the value "System.Data.Datarowview". When I change the line above by...

ValorTexto = Cb_Campo.Text;

...this variable receives the value "Select", as if nothing had been selected.

Can anyone tell me what I’m doing wrong? Thank you.

2 answers

0

That result System.Data.DataRowView means that, the property SelectedItem returned to you the selected object, which was set in the DataSource as you mentioned.

To catch a value on this object you need to convert to DataRowView and take the value of the correct column.

Your question doesn’t have your Datasource, but imagine that it is a table with the columns "Code" and "Description", and in this case you want to recover the value of "Code", could do so:

DataRowView dataRowView = (DataRowView)Cb_Campo.SelectedValue;
ValorChave = int.Parse(dataRowView.Row["Codigo"]);

0

My suggestion is that you create a class to store Key and Value. Then create a list of objects of this class and assign to the Datasource of your combo. Example of code below that should work in your form.

public class ConteudoCombo 
{
     public int Chave {get; set;}
     public string Nome {get; set;}
}

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    var conteudo = new List<ConteudoCombo>();
    conteudo.Add(new ConteudoCombo{Chave = 0, Nome = "SELECIONE"});
    conteudo.Add(new ConteudoCombo{Chave = 1, Nome = "OPÇÃO 1"});
    Cb_Campo.DisplayMember = nameof(ConteudoCombo.Nome);
    Cb_Campo.ValueMember = nameof(ConteudoCombo.Chave);
    Cb_Campo.DataSource = conteudo;
}

private void LerValorCombo()
{
    var conteudoSelecionado = Cb_Campo.SelectedItem as ConteudoCombo;
    ValorChave = conteudoSelecionado.Chave;
    ValorTexto = conteudoSelecionado.Nome;
}
  • Talles. I can’t do that, because the datasource already comes from a BD table. But the suggestion is interesting.

    1. Could you edit your question to include how Cb_field is filled? 2) Have you tried this option? Valuetext = Cb_field.Selectedtext;
  • Hello, Talles. What I did was based on Ricardo’s suggestion (below). Veja como ficou o trecho: &#xA; Cb_Campo.DisplayMember = CampoTexto;&#xA; Cb_Campo.ValueMember = CampoChave;&#xA; ValorChave = int.Parse(Cb_Campo.SelectedValue.ToString());&#xA; DataRowView dataRowView = (DataRowView)Cb_Campo.SelectedItem;&#xA; ValorTexto = dataRowView.Row[CampoTexto]. Tostring();

Browser other questions tagged

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