本文介绍了WinForm的组合框添加文本和QUOT;选取[数据绑定后,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我的状态,我有一个控制组合框。我想数据绑定添加文本后选择。我试试这个

  cbOperatorList.DataSource = operatorService.GetOperatorList();
cbOperatorList.Items.Insert(0,选择);
 

但是,当我做到这一点。我得到的例外是什么

更新

 公开的BindingList<操作> GetOperatorList(字符串过滤器=)
{
            返回
                新的BindingList<操作>(
                    this.operatorRepository.All.Where(
                        项目=> item.FirtsName.Contains(过滤器)|| item.LastName.Contains(过滤器)|| item.MiddleName.Contains(过滤器))。
                        了ToList());
}
 

更新

我解决了这一问题,使用该code

  cbOperatorList.DataSource =
                this.operatorService.GetOperatorList()。CONCAT(新[] {new运算符{姓氏=选择,ID = 0}})排序依据(
                    项目=> item.Id).ToList();
 

解决方案

如果 GetOperatorList()返回一个不可变的IEnumerable< T> ,你可以使用LINQ来连接与新的对象[] {选择} 。假设 T 不是对象,你会投:

  cbOperatorList.DataSource = operatorService
    .GetOperatorList()
    .Cast&其中;对象>()
    .Concat(新对象[] {选择});
 

修改

如果你想要的单词选择出现之初,反向串联:

  cbOperatorList.DataSource =(新对象[] {选择})
    .Concat(
        。operatorService.GetOperatorList()演员LT;对象>()
     );
 

At my form, I have one control ComboBox. I want after databinding add text "Select". I try this

cbOperatorList.DataSource = operatorService.GetOperatorList();
cbOperatorList.Items.Insert(0, "Select");

But when I do this. I get exception what

UPDATE

public BindingList<Operator> GetOperatorList(string filter = "")
{
            return
                new BindingList<Operator>(
                    this.operatorRepository.All.Where(
                        item => item.FirtsName.Contains(filter) || item.LastName.Contains(filter) || item.MiddleName.Contains(filter)).
                        ToList());
}

UPDATE

I fixed the problem, using this code

cbOperatorList.DataSource =
                this.operatorService.GetOperatorList().Concat(new[] { new Operator { LastName = "Select", Id = 0 } }).OrderBy(
                    item => item.Id).ToList();
解决方案

If GetOperatorList() returns an immutable IEnumerable<T>, you can use linq to concatenate that with new object[] { "Select" }. Assuming that T is not object, you would have to cast:

cbOperatorList.DataSource = operatorService
    .GetOperatorList()
    .Cast<object>()
    .Concat(new object[] { "Select" }); 

EDIT

If you want the word "Select" to appear at the beginning, reverse the concatenation:

cbOperatorList.DataSource = (new object[] { "Select" })
    .Concat(
        operatorService.GetOperatorList().Cast<object>()
     ); 

这篇关于WinForm的组合框添加文本和QUOT;选取[数据绑定后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 23:50