How do I programmatically add items to a list control?

You can programmatically add items to a list control as follows:

In C#:

// Create a new DataTable and DataColumns
DataTable dtList = new DataTable(); 
dtList.Columns.Add(new DataColumn("Display", typeof(string))); 
dtList.Columns.Add(new DataColumn("ID", typeof(int))); 

// Add three new rows to the list
dtList.Rows.Add(dtList.NewRow()); 
dtList.Rows.Add(dtList.NewRow()); 
dtList.Rows.Add(dtList.NewRow()); 

// Populate the rows
dtList.Rows[0][0] = "Apples"; 
dtList.Rows[0][1] = 1; 
dtList.Rows[1][0] = "Oranges"; 
dtList.Rows[1][1] = 2; 
dtList.Rows[2][0] = "Bananas"; 
dtList.Rows[2][1] = 3; 
 
// Databind the list control
comboBox1.DataSource = dtList; 
comboBox1.DisplayMember = "Display"; 
comboBox1.ValueMember = "ID";


And in VB .NET:

' Create a new DataTable and DataColumns
Dim dtList As New DataTable() 
dtList .Columns.Add(New DataColumn("Display", GetType(String))) 
dtList .Columns.Add(New DataColumn("ID", GetType(Integer))) 

' Add three new rows to the list
dtList.Rows.Add(dtList.NewRow()) 
dtList.Rows.Add(dtList.NewRow()) 
dtList.Rows.Add(dtList.NewRow()) 

' Populate the rows
dtList.Rows(0)(0) = "Apples" 
dtList.Rows(0)(1) = 1 
dtList.Rows(1)(0) = "Oranges"
dtList.Rows(1)(1) = 2
dtList.Rows(2)(0) = "Bananas"
dtList.Rows(2)(1) = 3

' Databind the list control
comboBox1.DataSource = dtList
comboBox1.DisplayMember = "Display" 
comboBox1.ValueMember = "ID"



© (c) 2026 Oak Leaf Enterprises, Inc., 1996-2026 • Updated: 01/13/05
Comment or report problem with topic