public static void SetDropDownList(ComboBox ddl, DropAddType type, DropAddFlag flag) { if (ddl == null) { return; } ArrayList items = new ArrayList(); if (type == DropAddType.New) { switch (flag) { case DropAddFlag.Select: items.Add(new ComboBoxItem("--请选择--", DropAddFlag.Select.ToString())); break; case DropAddFlag.All: items.Add(new ComboBoxItem("--选择全部--", DropAddFlag.All.ToString())); break; case DropAddFlag.Null: items.Add(new ComboBoxItem("", DropAddFlag.Null.ToString())); break; } } else { for (int i = 0; i < ddl.Items.Count; i++) { if (ddl.Items[i] is ComboBoxItem) { items.Add(ddl.Items[i]); } } } ddl.Items.Clear(); if (items.Count > 0) { ddl.DataSource = items; ddl.DisplayMember = "Name"; ddl.ValueMember = "Value"; } if (ddl.Items.Count > 0) { ddl.SelectedIndex = 0; } }
public static void SetDropDownList(ComboBox ddl, DataTable data, DropAddType type, DropAddFlag flag, string index, string colname) { if ((ddl == null) || (data == null)) { ddl.DataSource = new ArrayList(); return; } ArrayList items = new ArrayList(); if (type == DropAddType.New) { switch (flag) { case DropAddFlag.Select: items.Add(new ComboBoxItem("--请选择--", DropAddFlag.Select.ToString())); break; case DropAddFlag.All: items.Add(new ComboBoxItem("--选择全部--", DropAddFlag.All.ToString())); break; case DropAddFlag.Null: items.Add(new ComboBoxItem("", DropAddFlag.Null.ToString())); break; } } else { for (int i = 0; i < ddl.Items.Count; i++) { if (ddl.Items[i] is ComboBoxItem) { items.Add(ddl.Items[i]); } } } DataTableToItems(items, data, colname); //ddl.Items.Clear(); if (items.Count > 0) { ddl.DataSource = items; ddl.DisplayMember = "Name"; ddl.ValueMember = "Value"; } SelectDropDownList(ddl, index); }