C# 如何绑定 ComboBox 以便显示成员连接源数据表的 2 个字段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1006521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I bind a ComboBox so the displaymember is concat of 2 fields of source datatable?
提问by callisto
I'd like to bind a ComboBox
to a DataTable
(I cannot alter its original schema)
我想将 a 绑定ComboBox
到 a DataTable
(我无法更改其原始架构)
cbo.DataSource = tbldata;
cbo.DataTextField = "Name";
cbo.DataValueField = "GUID";
cbo.DataBind();
I want the ComboBox
show tbldata.Name + tbldata.Surname
.
我要ComboBox
演出tbldata.Name + tbldata.Surname
。
Of course adding the new name+surname as a field to the tbldata
just before binding is possible, but I am hoping for a more elegant solution along the lines of (pseudocode)
当然,tbldata
在绑定之前将新名称+姓氏作为字段添加到绑定是可能的,但我希望有一个更优雅的解决方案(伪代码)
cbo.DataTextField = "Name";
cbo.DataTextField += "Surname";
采纳答案by Jamie Ide
The calculated column solution is probably the best one. But if you can't alter the data table's schema to add that, you can loop through the table and populate a new collection that will serve as the data source.
计算列解决方案可能是最好的解决方案。但是,如果您无法更改数据表的架构以添加该架构,则可以遍历该表并填充将用作数据源的新集合。
var dict = new Dictionary<Guid, string>();
foreach (DataRow row in dt.Rows)
{
dict.Add(row["GUID"], row["Name"] + " " + row["Surname"]);
}
cbo.DataSource = dict;
cbo.DataTextField = "Value";
cbo.DataValueField = "Key";
cbo.DataBind();
Obviously this isn't as performant as binding directly to the DataTable but I wouldn't worry about that unless the table has thousands of rows.
显然,这不如直接绑定到 DataTable 那样高效,但除非表有数千行,否则我不会担心。
回答by bendewey
I would create a property on your data object then map that to the DataTextField
我会在您的数据对象上创建一个属性,然后将其映射到 DataTextField
Data Object
数据对象
public string FullName
{
get { return Name + " " + Surname; }
}
Code-behind
代码隐藏
cbo.DataSource = tbldata;
cbo.DataTextField = "FullName";
cbo.DataValueField = "GUID";
cbo.DataBind();
回答by SO User
Have a property in your class that is the concat of Name and Surname. And bind the DataTextField to this property.
在您的班级中有一个属性,它是 Name 和 Surname 的串联。并将 DataTextField 绑定到此属性。
In case you are binding it to a DataTable, you can add a new column to the DataTable whose values are concat of Name and Surname and bind it to the combo.
如果您将其绑定到 DataTable,您可以向 DataTable 添加一个新列,其值是 Name 和 Surname 的连接,并将其绑定到组合。
回答by Thomas Levesque
The easiest way is to create a new calculated column in the DataTable, using the Expression property :
最简单的方法是使用 Expression 属性在 DataTable 中创建一个新的计算列:
tbldata.Columns.Add("FullName", typeof(string), "Name + ' ' + Surname");
...
cbo.DataTextField = "FullName";
回答by Ajw
Have a look at calculated columns using the Expressionproperty of DataColumn.
使用DataColumn的Expression属性查看计算列。
回答by Tamir
You can register the combo binding event and iterate the items, setting each item text to the desired fields using the combobox item data item.
您可以注册组合绑定事件并迭代项目,使用组合框项目数据项将每个项目文本设置为所需的字段。
回答by anaconda
Or implement 'Format' event like this:
或者像这样实现 'Format' 事件:
DataRow r = ((DataRowView)e.ListItem).Row;
e.Value = r[ "FirstName" ] + " - " + r[ "LastName" ];