Alex Rivera | Logout

How to add item to the beginning of List<T>?

Asked 2008-12-24T00:34:38.313
550

I want to add a "Select One" option to a drop down list bound to a List<T>.

Once I query for the List<T>, how do I add my initial Item, not part of the data source, as the FIRST element in that List<T> ? I have:

// populate ti from data               
List<MyTypeItem> ti = MyTypeItem.GetTypeItems();    
//create initial entry    
MyTypeItem initialItem = new MyTypeItem();    
initialItem.TypeItem = "Select One";    
initialItem.TypeItemID = 0;
ti.Add(initialItem)  <!-- want this at the TOP!    
// then     
DropDownList1.DataSource = ti;
Edit
Report

1 Answer

936

Use the Insert method:

ti.Insert(0, initialItem);
answered 2008-12-24T00:37:58.590

Your Answer