.Net:我如何可以有一个组合框与其数据源的多重字段作为Displaymember? [英] .Net: how can I have a combobox with multitple fields of its data source as Displaymember?
问题描述
如何在不向数据源中添加其他列的情况下,将数据源的多个字段作为其显示成员的组合框?
How can I have a combobox with multiple fields of its data source as its display member without adding additional columns to its data source?
我的意思是一个带有ID+ - 名称的显示成员的组合框。
1 - 黑色
2 - 白色
3 - 红色
I mean I wanna to have a combobox with displaymember like "ID" + "-" "Name" .
1 - Black
2 - White
3 - Red
我不想在数据源中添加其他列。
I DO NOT want to add additional column to its data source.
谢谢
推荐答案
不支持绑定到多个属性(在WinForms中)。您必须用计算列扩展 DataTable
,并绑定到此新列。
Binding to multiple properties is not supported (in WinForms). You have to extend your DataTable
with a computed column and bind to this new column.
dataTable.Columns.Add("IdAndName", typeof(String), "ID + Name");
comboBox.DataSource = dataTable;
comboBox.DisplayMember = "IdAndName";
请参阅 MSDN 用于对计算列的有效表达式的引用。也许你必须使用 Convert
。
See the MSDN for reference on valid expressions for computed columns. Maybe you have to use Convert
.
dataTable.Columns.Add("IdAndName", typeof(String), "Convert(ID, 'System.String') + Name");
这篇关于.Net:我如何可以有一个组合框与其数据源的多重字段作为Displaymember?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!