Last active
February 9, 2018 20:19
-
-
Save anytizer/f0fc2575a99417c80d29dfb592c2fe3d to your computer and use it in GitHub Desktop.
Custom drop-down combo box in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DataDTO | |
{ | |
public string id { get; set; } | |
public string name { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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