Write ComboBox C # values in the database instead of descriptions

1

I have a screen in a simple registry form, where it has some combobox. I set your values manually.

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, how do I make time to write to the bank if it is option 1 - Limited it persists with the symbol L and if it is option 2 - Total it persists with the abbreviation F in the column of my table?

    
asked by anonymous 20.02.2017 / 00:44

5 answers

6

For you to have the expected result (in a "better" implementation), you need to define a class that represents an item in its 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 this answer .

    
20.02.2017 / 12:10
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;
    }
}

Usage:

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;   
    
20.02.2017 / 13:12
2

You can create a class containing the text and 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 usage:

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());
}
    
21.02.2017 / 13:13
1

The best way to do this is to use the DataSource property of the ComboBox, so the Combobox is already prepared to use the DataBinding, not to mention that if the key field value is of a different type of string, have to use casts by 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 you get the data from the combobox, just use the SelectedValue property of it:

int intCodigoSelecionado = (int) cboComboBox1.SelectedValue;
    
20.02.2017 / 13:09
0

Well from what I realized of the question you want a code like that

string escolha;

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

Then just use the variable of choice when it is inserted into the database.

    
20.02.2017 / 11:37