How can I programmatically create a new list for my ComboBox dropdown

Platform: WinForms| Category: ComboBox

Here are some snippets. (Courtesy of Michael Lang)

[C#]
	DataTable list = new DataTable();
	list.Columns.Add(new DataColumn('Display', typeof(string)));
	list.Columns.Add(new DataColumn('Id', typeof(int)));
	list.Rows.Add(list.NewRow());
	list.Rows.Add(list.NewRow());
	list.Rows.Add(list.NewRow());
	list.Rows[0][0] = 'one';
	list.Rows[0][1] = 1;
	list.Rows[1][0] = 'two';
	list.Rows[1][1] = 2;
	list.Rows[2][0] = 'three';
	list.Rows[2][1] = 3;
	comboBox1.DataSource = list;
	comboBox1.DisplayMember = 'Display';
	comboBox1.ValueMember = 'Id';

[VB.NET]
	Dim list As New DataTable()
	list.Columns.Add(New DataColumn('Display', GetType(String)))
	list.Columns.Add(New DataColumn('Id', GetType(Integer)))
	list.Rows.Add(list.NewRow())
	list.Rows.Add(list.NewRow())
	list.Rows.Add(list.NewRow())
	list.Rows(0)(0) = 'one' ’
	list.Rows(0)(1) = 1 ’
	list.Rows(1)(0) = 'two' ’
	list.Rows(1)(1) = 2 ’
	list.Rows(2)(0) = 'three' ’
	list.Rows(2)(1) = 3 ’
	comboBox1.DataSource = list
	comboBox1.DisplayMember = 'Display'
	comboBox1.ValueMember = 'Id'

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.