combobox加入请选择

pyunsong 2012-04-09 09:25:38
C#版中的combobox

cbbTempList.DataSource = BLL.CommonUtinl.GetTempByUserId(string.Format(" RF_User_Id={0} and Template_CreateTime='{1}'",this.loginUserID,"6"));

this.cbbTempList.Items.Insert(-1,"请选择"); 为何这句会报错?
cbbTempList.ValueMember = "Id";
cbbTempList.DisplayMember = "Template_Name";
this.cbbTempList.SelectedIndex = -1;
...全文
459 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
exception92 2012-04-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
而且你的程序顺序好像错了吧~

C# code


cbbTempList.ValueMember = "Id";
cbbTempList.DisplayMember = "Template_Name";
cbbTempList.DataSource = BLL.CommonUtinl.GetTempByUserId(string.Format(" RF_User_Id={0} an……
[/Quote]

+1
全局变量 2012-04-09
  • 打赏
  • 举报
回复
winform ??
这的
ListItem listItem0 = new ListItem("0", "选项零");
comboBox1.Items.Add(listItem0);
pyunsong 2012-04-09
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 的回复:]

this.cbbTempList.Items.insert(0,new ListItem(""---请选择---"", "0"));
[/Quote]

好象winform 下好象没有listItem这个写法吧,报错,您可以试下。。

在webform是有listItem,可以这样操作!
全局变量 2012-04-09
  • 打赏
  • 举报
回复
this.cbbTempList.Items.insert(0,new ListItem(""---请选择---"", "0"));
pyunsong 2012-04-09
  • 打赏
  • 举报
回复
是从0开始的。。但显示的时候不会出来了 请选择 这一项哟。。。。

this.cbbTempList.Items.insert(0,"---请选择---");
cbbTempList.DataSource = BLL.CommonUtinl.GetTempByUserId(string.Format(" RF_User_Id={0} and Template_CreateTime='{1}'", this.loginUserID, "6"));
cbbTempList.ValueMember = "RF_Template_Id";
cbbTempList.DisplayMember = "Template_Name";
this.cbbTempList.SelectedIndex = 0;
弯月满弦 2012-04-09
  • 打赏
  • 举报
回复
-1 是没有行的,需要插入的话从0开始
pyunsong 2012-04-09
  • 打赏
  • 举报
回复
不会出现"请选择" 这一项。。

this.cbbTempList.Items.Add("---请选择---");
cbbTempList.DataSource = BLL.CommonUtinl.GetTempByUserId(string.Format(" RF_User_Id={0} and Template_CreateTime='{1}'", this.loginUserID, "6"));
cbbTempList.ValueMember = "RF_Template_Id";
cbbTempList.DisplayMember = "Template_Name";
this.cbbTempList.SelectedIndex = 0;


或是

this.cbbTempList.Items.insert(0,"---请选择---");
cbbTempList.DataSource = BLL.CommonUtinl.GetTempByUserId(string.Format(" RF_User_Id={0} and Template_CreateTime='{1}'", this.loginUserID, "6"));
cbbTempList.ValueMember = "RF_Template_Id";
cbbTempList.DisplayMember = "Template_Name";
this.cbbTempList.SelectedIndex = 0;
chokobo 2012-04-09
  • 打赏
  • 举报
回复
在datasource里先insert你要加入的“请选择”,再绑定到控件
梦世界 2012-04-09
  • 打赏
  • 举报
回复
this.cbbTempList.Items.Insert(0,"请选择");放最前面 ,还有
this.cbbTempList.Items.add("请选择"),不也行吗?
orochiheart 2012-04-09
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

this.cbbTempList.Items.Insert(0,"请选择");放最前面
[/Quote]
+1 MEYBE
全局变量 2012-04-09
  • 打赏
  • 举报
回复
cbbTempList.DataBind()这句是不是漏了
全局变量 2012-04-09
  • 打赏
  • 举报
回复
this.cbbTempList.Items.Insert(0,"请选择");放最前面
pyunsong 2012-04-09
  • 打赏
  • 举报
回复
items collection cannot be modified when the datasource property is set

报这个错。。不能同时双向绑定。。改成0也是一样的报这个错。
去掉this.cbbTempList.Items.Insert(0,"请选择");这句一切正常!
wangluojianke 2012-04-09
  • 打赏
  • 举报
回复
this.cbbTempList.Items.Insert(0,"请选择");
全局变量 2012-04-09
  • 打赏
  • 举报
回复
而且你的程序顺序好像错了吧~

cbbTempList.ValueMember = "Id";
cbbTempList.DisplayMember = "Template_Name";
cbbTempList.DataSource = BLL.CommonUtinl.GetTempByUserId(string.Format(" RF_User_Id={0} and Template_CreateTime='{1}'",this.loginUserID,"6"));
this.cbbTempList.Items.Insert(0,"请选择");
this.cbbTempList.SelectedIndex = 0;
全局变量 2012-04-09
  • 打赏
  • 举报
回复
我只知道程序都是从0或1开始~ 从没见过从-1开始的。 -1一般都是代表错误找不到之类的把。
色拉油 2012-04-09
  • 打赏
  • 举报
回复
在绑定之前对数据源insert(),这样就可以放在最前了
orochiheart 2012-04-09
  • 打赏
  • 举报
回复
报什么错了?查MSDN
orochiheart 2012-04-09
  • 打赏
  • 举报
回复
楼上nice 简单实用
EnForGrass 2012-04-09
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 的回复:]

没有搞定。。。。。。。。。。。。
[/Quote]
到数据源中添加,比如datatable。给个例子吧

DataTable dt = new DataTable();
dt.Columns.Add("DetpName", typeof(string));
dt.Columns.Add("MemberName", typeof(string));
DataRow dr1 = dt.NewRow();
dr1["DetpName"] = "张";
dr1["MemberName"] = "AAA";
dt.Rows.Add(dr1);

DataRow dr2 = dt.NewRow();
dr2["DetpName"] = "李";
dr2["MemberName"] = "BBB";
dt.Rows.Add(dr2);

DataRow dr3 = dt.NewRow();
dr3["DetpName"] = "王";
dr3["MemberName"] = "CCC";
dt.Rows.Add(dr3);

DataRow dr4 = dt.NewRow();
dr4["DetpName"] = "曹";
dr4["MemberName"] = "DDD";
dt.Rows.Add(dr4);

DataRow dr5 = dt.NewRow();
dr5["DetpName"] = "徐";
dr5["MemberName"] = "EEE";
dt.Rows.Add(dr5);

DataRow dr6 = dt.NewRow();
dr6["DetpName"] = "杨";
dr6["MemberName"] = "FFF";
dt.Rows.Add(dr6);

DataRow dr7 = dt.NewRow();
dr7["DetpName"] = "孙";
dr7["MemberName"] = "GGG";
dt.Rows.Add(dr7);

DataRow dr8 = dt.NewRow();
dr8["DetpName"] = "赵";
dr8["MemberName"] = "HHH";
dt.Rows.Add(dr8);

DataRow dr = dt.NewRow();
dr["DetpName"] = "0";
dr["MemberName"] = "请选择";
dt.Rows.InsertAt(dr, 0);

this.comboBox1.DataSource = dt;
this.comboBox1.DisplayMember = "MemberName";
加载更多回复(1)

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧