winformにおけるcomboxの運用


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void bindDate()
        {
            ArrayList arrayList = new ArrayList();
            string[] posMsg = { "1,a", "2,b" };
            for (int i = 0; i < posMsg.Length; i++)
            {
                string[] oneMsg;
                oneMsg = posMsg[i].Split(',');
                arrayList.Add(new DictionaryEntry(oneMsg[0], oneMsg[1]));
            }
            comboBox1.DataSource = arrayList;
            comboBox1.ValueMember = "Key";
            comboBox1.DisplayMember = "Value";

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            bindDate();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedValue != null)
            {
               label1.Text = comboBox1.SelectedValue.ToString();
                label2.Text = comboBox1.Text;
            }
        }
    }
}

 
ComboBoxのフォーマット:
       cboTimeStamp.FormatString = Config.AppConfig.DateTimeFormat;
 
 public void BindProduct(List<MaterialDataView> materialList)
        {
            if (materialList.Where(x => x.Name == ControlUtil.EmptyItem).ToList().Count == 0)
            {
                MaterialDataView m = new MaterialDataView() { Name = ControlUtil.EmptyItem, ID = 0, };
                materialList.Insert(0, m);
            }
            this.cbxProduct.DataSource = materialList;
            this.cbxProduct.DisplayMember = "Name";
            this.cbxProduct.ValueMember = "ID";
            this.cbxProduct.SelectedIndex = 0;
        }
 ControlUtil.BindComboBoxWithEmpty(cboArea, AreaList, "Name", "Code");

     public static void BindComboBoxWithEmpty<T>(ComboBox cbo, List<T> list, string displayMember, string valueMember) where T : new()
        {
            BindComboBoxWithEnum(cbo, list, displayMember, valueMember, ComboBoxDefaultValue.Empty);
        }

     public static void BindComboBoxWithEnum<T>(ComboBox cbo, List<T> list, string displayMember, string valueMember,ComboBoxDefaultValue defaultValue) where T : new()
        {
            cbo.DataSource = null;

            List<T> listT = new List<T>();
            foreach (T item in list) listT.Add(item);

            cbo.DisplayMember = displayMember;
            cbo.ValueMember = valueMember;

            T t = new T();

            if (defaultValue != ComboBoxDefaultValue.Empty)
                t.GetType().GetProperty(displayMember).SetValue(t, defaultValue.ToString(),null);

            listT.Insert(0, t);

            cbo.DataSource = listT;
        }



    public enum ComboBoxDefaultValue
    {
        Empty,
        All
    }

---------------------------------------------------------
  DataSourceUtil.BindComboBoxWithDefaultValue(cboMaterial, RecipeDetailModel.MaterialList, "Name", "ID", 0);
            if (!string.IsNullOrEmpty(RecipeId.ToString()))
            {
                foreach (var item in cboMaterial.Items)
                {
                    MD_Material material = item as MD_Material;
                    if (material != null && material.ID == RecipeDetailModel.Material.ID)
                    {
                        cboMaterial.SelectedItem = item;
                        break;
                    }
                }
                SetTotalRatio();
            }