如何添加项目到List< T>? [英] How to add item to the beginning of List<T>?

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

问题描述

我想在绑定到列表< T> 的下拉列表中添加一个选择一个选项。

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

一旦我查询列表< T> ,我如何添加我的初始 Item ,而不是数据源的一部分,作为 List< T> 中的FIRST元素?我有:

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&lt; T&gt;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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