如何绑定一个组合这样的DisplayMember是源数据表的2场CONCAT? [英] How do I bind a Combo so the displaymember is concat of 2 fields of source datatable?
问题描述
我还想组合绑定到一个DataTable(我不能改变它的ITO原始模式)
cbo.DataSource = tbldata;
cbo.DataTextField =姓名;
cbo.DataValueField =GUID;
cbo.DataBind();
我想要做什么,是有组合文本框显示tbldata.Name柱+ tbldata.Surname。
当然添加新名称+姓作为一个字段tbldata只是之前绑定是可能的,但我希望沿的线条更优雅的解决方案(伪code)
cbo.DataTextField =姓名;
cbo.DataTextField + =姓氏;
计算列的解决方案可能是最好的之一。但是,如果你不能改变数据表的模式补充一点,你可以通过表环和填充新的集合,将作为数据源。
VAR字典=新词典< GUID,可串>();
的foreach(在dt.Rows的DataRow行)
{
dict.Add(行[GUID],行[名称] ++行[姓]);
}
cbo.DataSource =字典;
cbo.DataTextField =值;
cbo.DataValueField =键;
cbo.DataBind();
显然,这不是因为直接绑定到数据表,但我不会担心,除非表有上千行的高性能。
Id like to bind a combo to a datatable (which I cannot alter ito it original schema)
cbo.DataSource = tbldata;
cbo.DataTextField = "Name";
cbo.DataValueField = "GUID";
cbo.DataBind();
What I want to do, is have the combo textfield show tbldata.Name column + 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)
cbo.DataTextField = "Name";
cbo.DataTextField += "Surname";
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.
这篇关于如何绑定一个组合这样的DisplayMember是源数据表的2场CONCAT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!