3

Edit: I'll try to be more specific.

In Visual Studio, it is easy to bind the items and values of a combobox to 2 columns in a DB.

       Index  |  Name
      ...................
        0     |  Apples
        1     |  Oranges
        2     |  Pears
  • But if one enters the names manually under Items in the Properties window.
  • And one saves only the index of the combobox to the DB.

      Index
    .........
       0     
       1     
       2     
    

So the question is how does one bind only the index of the combobox to the DB while using the Items entered in the Properties window as the displayed items?

Yster
  • 3,147
  • 5
  • 32
  • 48
  • @K.Dᴀᴠɪs Seems that you think it's a stupid question. If that is the case, why not just answer it and take the 50 reputation? – Yster Feb 14 '18 at 00:12
  • 1
    no that’s not where I was going. But your question is extremely broad and shows no effort whatsoever, which is why most questions are closed – K.Dᴀᴠɪs Feb 14 '18 at 00:26
  • 2
    WinForms, WPF? Do you use MVVM? If the comboxbox shows values from a DB wouldn't it be better to return the DB-Id of your DBTable instead the index of the combobox? Did you try anything for yourself? – GreenEyedAndy Feb 14 '18 at 12:33
  • @GreenEyedAndy Thank you for the response. I just use normal Visual Studio 15. The index of the combobox should be saved in the DB. So when the Form is loaded, the combobox should display the corresponding string. If 1 in stored in the DB, Oranges should be displayed. Then if the user choses Pears, 2 should be stored in the DB. This is the most basic way a combox is used. There is nothing more simple than this. I have nothing to show because this should be done in the IDE without any code. – Yster Feb 14 '18 at 16:52
  • @Yster, please check the answer I posted – Tarun Lalwani Feb 15 '18 at 14:58

2 Answers2

1
  • After one week and even a bounty, this question was not answered.
  • This is the most common and most basic way to use a comboBox.
  • Seems one can only do this by writing some custom code:

    private void MyForm_Load(object sender, EventArgs e)
    {
        Dictionary<int, string> myDic = new Dictionary<int, string>();
        myDic.Add(0, "Zero");
        myDic.Add(1, "One");
        myDic.Add(2, "Two");
        comboBox1.DataSource = new BindingSource(myDic, null);
        comboBox1.DisplayMember = "Value";
        comboBox1.ValueMember = "Key";
        comboBox1.DataBindings.Add("SelectedValue", bindingSource1, "Numbers");
    }
    
  • Only the index (or SelectedValue) of the comboBox is bound to the DB.
  • The last line of code can also be done in the Properties window (under DataBindings).
  • One cannot add the item texts in the Properties window.
  • One can only add the texts and values together as a key/value pair dictionary entry. (Or one can create a class for the entry).
  • Thanks to those who tried to answer the question.
  • Hope this will save someone some time.
Yster
  • 3,147
  • 5
  • 32
  • 48
  • I still not understand what you want. Your code is totally different from your requirements. E.g. your index is NOT bound to the DB, cause you have a local dictionary! I strongly recommend that you read some articles about databinding and maybe MVVM. It looks like you want to use WinForms. Take a look: https://learn.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-bind-a-windows-forms-combobox-or-listbox-control-to-data – GreenEyedAndy Feb 16 '18 at 17:42
  • @GreenEyedAndy Thanks for asking, but I need nothing else. The problem is solved. The index of the combobox is the SelectedValue. Items are added to the combobox by using a "local" dictionary. Nothing is bound to the dictionary. The values of the items in the combobox (which are also the indexes) are bound to a DB using bindingSource1. In the DB we only have the index. No need to store the strings as well. – Yster Feb 16 '18 at 22:19
0

I think this is duplicate of below question if you want to display a altered value

How to append two field values in combobox display member in C#

Depending on whether the value comes from DB or is a fixed array then you would just need to set these fields accordingly.

You should also look at the below article if you need to better understand how to DataSource can be used

http://www.c-sharpcorner.com/UploadFile/0f68f2/programmatically-binding-datasource-to-combobox-in-multiple/

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
  • Thanks so much for this answer. I can only go through it tomorrow and will get back to you as soon as possible. – Yster Feb 15 '18 at 19:32
  • @ Tarun Lalwani: This does not seem to be a duplicate to that question. Thanks for trying. – Yster Feb 16 '18 at 12:39