DropDownList数据值文本可以连接多个查询结果吗? [英] Can DropDownList Data Value text be concatenated by several query results?
问题描述
假设我在SQL Server数据库中有以下两个表:
Table_Order
- idOrder(int)
- OrderNumber(int)
- fk_Customer(int)
Table_Customer
- idCustomer(int )
- 客户名称(nchar)
Table_Order 中有一个外键fk_Customer指向主要 Table_Customer 中的key idCustomer,以便我可以为订单提取关联的CustomerName。
因此,我有一个显示订单的DropDownList绑定到它的属性:
- 数据值字段:idOrder
- 数据文本字段:OrderNumber
In DropDownList,是否也可以显示关联的CustomerName以及OrderNumber,例如:
Suppose I have the following two tables in SQL Server database:
Table_Order
- idOrder (int)
- OrderNumber (int)
- fk_Customer (int)
Table_Customer
- idCustomer (int)
- CustomerName (nchar)
There is a foreign key fk_Customer in Table_Order that points to the primary key idCustomer in Table_Customer, so that I can pull associated CustomerName for an order.
So, I have a DropDownList that shows Orders with following attributes bound to it:
- Data Value Field: idOrder
- Data Text Field: OrderNumber
In the DropDownList, is it possible to also show the associated CustomerName along with the OrderNumber, for example:
DDL.DataTextField = OrderNumber + " (" + CustomerName + ");
推荐答案
首先是选择查询:
first the select query:
select idorder, convert(varchar, ordernumber) + " (" + customername + ")" as orderno from order join customer on fk_customer = idcustomer
然后是ddl:
then the ddl:
Data Text Field: orderno
你好朋友,实现你必须根据需要在SQL级别创建一个连接两列的字段,如下所示:
Hello friend, to achieve that you have to create a field concatenating two columns as per your need at the SQL level as follows:
SELECT O.idOrder, O.OrderNumber + '(' + C.CustomerName + ')' AS Order_Customer
FROM TableCustomer C INNER JOIN TableOrder O ON C.idCustomer = O.fk_Customer
然后你可以设置 DataTextField
,如下所示:
Then you can set DataTextField
as shown below:
DDL.DataTextField = "Order_Customer"
- DD
- DD
这篇关于DropDownList数据值文本可以连接多个查询结果吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!