在组合框C#默认值数据源 [英] Default value DataSource in ComboBox C#
问题描述
我有一个组合框,那就是我如何填写它的数据:
I have a ComboBox, and that is how I fill the data in it:
SectorCollection sectorCollection = sectorController.SearchAll();
comboSector.DataSource = null;
comboSector.DataSource = sectorCollection;
comboSector.DisplayMember = "titleSector";
comboSector.ValueMember = "idSector";
我要的是设置前的数据,就像没有值在组合框中的文本。
像选择一个部门。这样用户就可以知道他是否被选择。
What I want is to set a pre data, like a text in the combobox without a value. Like "Select a Sector." So the user can knows what does he is selecting.
推荐答案
如果您使用的是WinForm的组合框,那么你应该在代码这样的事情
If you are using a WinForm combobox then you should code something like this
sectorCollection.Insert(0, new Sector() {idSector=0, titleSector="Select a sector"})
comboSector.DataSource = sectorCollection;
comboSector.DisplayMember = "titleSector";
comboSector.ValueMember = "idSector";
您需要添加选择提示为一个新的行业
实例添加到集合,然后收集绑定到组合框。当然,这可能是一个问题,如果你使用集合用于其他目的从组合显示
You need to add the selection prompt as a new Sector
instance added to the collection and then bind the collection to your combobox. Of course this could be a problem if you use the collection for other purposes a part from the combo display
这篇关于在组合框C#默认值数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!