How to Bind List<T> to List box and Dropdown List

Introduction

This tutorial will explain how to bind List<T> to the listbox/dropdown list. Following are some simple steps to bind your listbox/dropdown list to List<T>.

Step 1. Create a List<T> in you code behind file i.e. .CS file. Following code shows how the List<T> of string is created. Months is a List<String> which stores a list of months in a year.

/// Creating a list of strings for months.
public static List<string> Months = new List<string>
{
    "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"
};

Step 2. After creating List<T>, bind List<T> with your listbox/dropdown list. The following code binds to dropdown and listbox too.

// Binding a list of strings to a ListBox
lst.DataSource = Months;
lst.DataBind();

// Binding a list of strings to a DropDownList
drp.DataSource = Months;
drp.DataBind();

Output. When you run your application, the List<T> is bound to the listbox/dropdown list.

ListBox.gif


Similar Articles