c# - 组合 DataTextField 中的两个字段。这可能吗?

标签 c# asp.net

我有一个要绑定(bind)到列表框的数据集。但是,我想组合两个字段来组成 DataTextField。这可能吗,还是我必须循环遍历数据行?

lstAddressDropdown.DataSource = dsAddress;
lstAddressDropdown.DataTextField = "StreetAddress" + "Place";
lstAddressDropdown.DataBind();
lstAddressDropdown.Items.Insert(0, new ListItem("Please select"));

值得注意的是,数据集是从 Web 服务返回的,因此我无法更改任何存储过程来组合那里的列。

最佳答案

您可以向作为计算列的数据表添加一个附加列,并将其用作您的数据文本字段 ( docs: Multi-field Data Binding)。

因此对于上面的示例,您可以执行以下操作:

dsAddress.Tables[0].Columns.Add("StreetAndPlace",typeof(string),"StreetAddress + Place");
lstAddressDropdown.DataSource = dsAddress;
lstAddressDropdown.DataTextField = "StreetAndPlace";
lstAddressDropdown.DataBind();
lstAddressDropdown.Items.Insert(0, new ListItem("Please select"));

要在 StreetAddress 和 Place 之间添加一个空格,请将上面显示的表达式字符串替换为 "StreetAddress + ' ' + Place"

关于c# - 组合 DataTextField 中的两个字段。这可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4217481/

相关文章:

javascript - 正确使用调用c#函数的引导开关(T4MVC/Entity Framework)

c# - 如何设置 System.Data.SQLite.dll

asp.net - 如何在 asp.net mvc 2 中创建饼图

c# - EF 创建数据库并在其名称中添加 guid

c# - IQueryable 列表

c# - 是否可以使用 IndexOf 搜索 List<struct> 中的单个字段?

c# - 在 LINQ to SQL 中选择 "IN"

c# - 加密和解密 RESTful WCF Web 服务发送和接收的 XML

asp.net - Linq To Sql Intellisense 属性名称

javascript - 需要JS正则表达式来表示以下字符串