Save Combobox C# values to database instead of descriptions

Asked

Viewed 1,114 times

1

I have a screen in a simple registration form, where it has some combobox. I set its values manually same.

EX; In a combo that will register the access profile of a user, I have in this combo the option 1- Limited, 2 - total. However, as I do for times that record in the bank if it is option 1 - Limited it persists with the acronym L and if it is option 2 - Total it persists with the acronym F in the column of my table?

5 answers

6


To get the expected result (in a "better" implementation), you need to define a class that represents an item in your comboBox. Ex:

public class MeuComboBoxItem 
{
    public string TextoDoItem { get; set; }
    public object ValorDoItem { get; set; }

    string  public override string ToString()
    {
        return TextoDoItem;
    }
}

Populating the combo:

MeuComboBoxItem item = new MeuComboBoxItem();
item.TextoDoItem = "1- Limitado";
item.ValorDoItem = "L";

comboBox1.Items.Add(item);

To redeem the value of the selected item:

string valorDoItemSelectionado = (comboBox1.SelectedItem as MeuComboBoxItem).ValorDoItem.ToString();

Response based in that reply.

  • 1

    Pow man, very good I will test...that’s exactly what I needed. Top.

  • Cool! I’m glad I could help! :-)

2

You can create a class containing the text and the value and assign it to the combobox. Example:

public class ComboboxItem
{
    public string Text { get; set; }
    public object Value { get; set; }

    public override string ToString()
    {
        return Text;
    }
}

Example of use:

private void Test()
{
   ComboboxItem item = new ComboboxItem();
   item.Text = "Item text1";
   item.Value = 12;

   comboBox1.Items.Add(item);

   comboBox1.SelectedIndex = 0;

   MessageBox.Show((comboBox1.SelectedItem as   ComboboxItem).Value.ToString());
}
  • Caio, thanks for the reply, helped too...

2

You can create a list and assign it to the combobox.

public sealed class ComboBoxItem
{
    public string Texto { get; set; }
    public string Valor { get; set; }

    public override string ToString()
    {
        return this.Texto;
    }
}

Use:

var itens = new List<ComboBoxItem>();
itens.Add(new ComboBoxItem { Valor = "1", Texto = "Ola" });
itens.Add(new ComboBoxItem { Valor = "2", Texto = "Oi" });

cbo.DataSource = itens;
cbo.DisplayMember = "Texto";
cbo.ValueMember = "Valor";

Redeem selected value:

var item = (ComboBoxItem)cbo.SelectedItem;   

1

The best way to do this is by using the Datasource property of Combobox, this way Combobox is already prepared to use Databinding, not to mention that if the value of the key field is of a different type of string, avoid having to use Casts for the code, see an example below:

// ***** Exemplo 1 - Utilizando uma List
// Neste exemplo utilizarei uma lista de KeyValuePar para identificar os meus itens.

var lstData = new List<KeyValuePair<int, string>>
{
    new KeyValuePair<string, string>(1, "Valor 1"),
    new KeyValuePair<string, string>(2, "Valor 2"),
    new KeyValuePair<string, string>(3, "Valor 3"),
    new KeyValuePair<string, string>(4, "Valor 4")
};

cboComboBox1.DataSource = null;
cboComboBox1.Items.Clear();
// Utilizo um BindingSource para "bindar os dados com os itens do Combobox"
cboComboBox1.DataSource = new BindingSource(lstData, null);
// Aqui fala qual será o campo a ser exibido
cboComboBox1.DisplayMember = "Value";
// Aqui fala qual campo será selecionado
cboComboBox1.ValueMember = "Key";


// ***** Exemplo 2 - Utilizando uma List de um objeto
// Neste exemplo utilizarei uma lista de KeyValuePar para identificar os meus itens.
class ObjetoTeste 
{
    public ObjetoTeste (int codigo, string descricao)
    {
        this.Codigo = codigo;
        this.Descricao = descricao;
    }

    public int Codigo { get; private set; } 
    public string Descricao { get; private set; }
}

var lstData = new List<ObjetoTeste>
{
    new ObjetoTeste(1, "Valor 1"),
    new ObjetoTeste(2, "Valor 2"),
    new ObjetoTeste(3, "Valor 3"),
    new ObjetoTeste(4, "Valor 4")
};

cboComboBox1.DataSource = null;
cboComboBox1.Items.Clear();
// Utilizo um BindingSource para "bindar os dados com os itens do Combobox"
cboComboBox1.DataSource = new BindingSource(lstData, null);
// Aqui fala qual será o campo a ser exibido
cboComboBox1.DisplayMember = "Value";
// Aqui fala qual campo será selecionado
cboComboBox1.ValueMember = "Key";

When picking up the combobox data just use the Selectedvalue property of the combobox:

int intCodigoSelecionado = (int) cboComboBox1.SelectedValue;

0

Well from what I understand of the question you want a code like this

string escolha;

if (comboBox1.Text == "Limitado")
{
    escolha = "L";
}    
else if (comboBox1.Text == "Total")
{
    escolha = "F";
}

Then just use the choice variable when inserted into the database.

Browser other questions tagged

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