Skip to content

Instantly share code, notes, and snippets.

@anytizer
Last active February 9, 2018 20:19
Show Gist options
  • Save anytizer/f0fc2575a99417c80d29dfb592c2fe3d to your computer and use it in GitHub Desktop.
Save anytizer/f0fc2575a99417c80d29dfb592c2fe3d to your computer and use it in GitHub Desktop.
Custom drop-down combo box in C#
dbEntities je = new dbEntities();
List<DataDTO> ds = new List<DataDTO>();
foreach (var p in je.purposes)
{
DataDTO d = new DataDTO();
d.id = p.purpose_id;
d.name = p.purpose_code;
ds.Add(d);
}
// assign array
comboBox1.Items.Clear();
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox1.DataSource = new BindingSource(DataDTO[] d, null);
comboBox1.DisplayMember = "name";
comboBox1.ValueMember = "id";
public class DataDTO
{
public string id { get; set; }
public string name { get; set; }
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DataDTO d = (DataDTO)comboBox1.SelectedItem;
MessageBox.Show(d.id);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment