WinForm ComboBox add text “Select” after data binding

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 07:25:26

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>()
     ); 

You don't describe what GetOperatorList() returns, but you could first set a variable to get that list and insert your item in the list before setting the DataSource to that variable.

You would have to refactor your code to handle this "Select" item to not get confused with your operator objects.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!