0

I am trying to make combobox with DisplayMember and ValueMember option I want to discuss the available options to that

Option 1

using DataTable as DataSource and load it into DisplayMember and ValueMember and that option will avoid me the hardcode science data are load it from the database

This option is not applicable in my case for several reason example I have not granted the create table permission

Option 2

using class as shown in this example but that has disadvantage of hardcode

Option 3

using Dictionary as below

Dictionary<string,>test = new Dictionary<string,>();
        test.Add("1", "example1");
        test.Add("2", "example2");
        test.Add("3", "example3");
        comboBox1.DataSource = new BindingSource(test, null);
        comboBox1.DisplayMember = "Value";
        comboBox1.ValueMember = "Key";

// Get combobox selection (in handler)
string value = ((KeyValuePair<string,>)comboBox1.SelectedItem).Value;

to read values back

DictionaryEntry deImgType = (DictionaryEntry)cmbImageType.SelectedItem;
MessageBox.Show(deImgType.Key + ": " + deImgType.Value);

is there any other options? and may this option avoid me the hardcode pelsae ?

Thanks

sam
  • 2,493
  • 6
  • 38
  • 73
  • 1
    What is this hardcode that seems to scare you so much? It always amazes me how much effort people will put into avoiding a trivial task. Also, you do not need an external database system to create and fill a DataTable. – TnTinMn Sep 16 '18 at 17:25
  • All the options are similar, using a data source and setting display member and value member. What's the actual problem which you are trying to solve? Avoid hard-coding what? – Reza Aghaei Sep 16 '18 at 20:45
  • Option 2, you can create class objects without hard-coding anything. – Jack Le Sep 17 '18 at 02:04

0 Answers0