541

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;

5 Answers 5

922

Use the Insert method:

ti.Insert(0, initialItem);
6
  • Would it be possible to insert at the end of the list? Commented Dec 11, 2017 at 14:36
  • 8
    @GaryHenshall yes, use the Add method, it inserts at the end. Commented Jan 2, 2018 at 8:19
  • 19
    Since .NET 4.7.1, you can use Append() and Prepend(). Check this answer
    – aloisdg
    Commented Jan 8, 2020 at 10:13
  • 1
    Wouldnt this replace an existing value that is already in the list? Commented Jul 30, 2021 at 7:00
  • 2
    No this does not replace the first item
    – Pieter
    Commented Jan 12, 2022 at 15:39
47

Since .NET 4.7.1, you can use the side-effect free Prepend() and Append(). The output is going to be an IEnumerable.

// Creating an array of numbers
var ti = new List<int> { 1, 2, 3 };

// Prepend and Append any value of the same type
var results = ti.Prepend(0).Append(4);

// output is 0, 1, 2, 3, 4
Console.WriteLine(string.Join(", ", results));

Edit:

If you want to explicitly mutate your given list:

// Creating an array of numbers
var ti = new List<int> { 1, 2, 3 };

// mutating ti
ti = ti.Prepend(0).ToList();

but at that point just use Insert(0, 0)

4
  • Depends on the use-case. Prepend() does something else, much better in some situations but will be unacceptable in others. Commented Jan 17, 2023 at 9:38
  • @HH Prepend() adds a value to the beginning of the sequence. Note that the original sequence is not altered. The result is a whole new sequence. The Prepend's documentation shows a lot of useful example.
    – aloisdg
    Commented Jan 17, 2023 at 10:49
  • 2
    Yes, but a "a whole new sequence" is not what was asked. Commented Jan 17, 2023 at 13:10
  • @HH Well you cant still mutate the list by re-assign it, but at the point you might as well use Insert...
    – aloisdg
    Commented Jan 17, 2023 at 13:51
26

Update: a better idea, set the "AppendDataBoundItems" property to true, then declare the "Choose item" declaratively. The databinding operation will add to the statically declared item.

<asp:DropDownList ID="ddl" runat="server" AppendDataBoundItems="true">
    <asp:ListItem Value="0" Text="Please choose..."></asp:ListItem>
</asp:DropDownList>

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.appenddatabounditems.aspx

-Oisin

0
8

Use Insert method of List<T>:

List.Insert Method (Int32, T): Inserts an element into the List at the specified index.

var names = new List<string> { "John", "Anna", "Monica" };
names.Insert(0, "Micheal"); // Insert to the first element
8

Use List<T>.Insert

While not relevant to your specific example, if performance is important also consider using LinkedList<T> because inserting an item to the start of a List<T> requires all items to be moved over. See When should I use a List vs a LinkedList.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.