如何将项目添加到 List<T> 的开头? [英] How to add item to the beginning of List&lt;T&gt;?

查看:29
本文介绍了如何将项目添加到 List<T> 的开头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向绑定到 List 的下拉列表添加选择一个"选项.

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

一旦我查询了 List,我如何添加我的初始 Item,而不是数据源的一部分,作为该 列表 ?我有:

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;

推荐答案

使用 插入方法:

ti.Insert(0, initialItem);

这篇关于如何将项目添加到 List<T> 的开头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆