如何添加项目名单,LT的开始; T&GT ;? [英] How to add item to the beginning of List<T>?

查看:145
本文介绍了如何添加项目名单,LT的开始; T&GT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要添加一个选择一项选项,下拉列表绑定到一个列表< T>

有一次,我查询了列表< T> ,我怎么加我最初的项目,而不是一部分数据源,作为第一元素列表< T> ?我有:

  //从数据填充TI
清单< MyTypeItem> TI = MyTypeItem.GetTypeItems();
//创建初始入口
MyTypeItem initialItem =新MyTypeItem();
initialItem.TypeItem =选择一;
initialItem.TypeItemID = 0;
ti.Add(ini​​tialItem)LT; - !希望这个顶部!
// 然后
DropDownList1.DataSource = TI;


解决方案

使用插入方法:

  ti.Insert(0,initialItem);

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;

解决方案

Use the Insert method:

ti.Insert(0, initialItem);

这篇关于如何添加项目名单,LT的开始; T&GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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