tailieunhanh - Displaying Columns from a Related DataTable

[ Team LiB ] Recipe Displaying Columns from a Related DataTable Problem You want to add a column to a DataTable that displays a value from a row in a related table in the DataSet. Solution Use expression columns to retrieve lookup values based on DataRelation objects. | Team LiB Recipe Displaying Columns from a Related DataTable Problem You want to add a column to a DataTable that displays a value from a row in a related table in the DataSet. Solution Use expression columns to retrieve lookup values based on DataRelation objects. The sample code creates a new DataSet containing the Orders table and the Order Details table from Northwind. A DataRelation is created between the tables. A column is added to the Order Details table that gets the CustomerlD from the parent Order using the relation created between the tables. Finally the default view of the Orders table is bound to the data grid on the form. The C code is shown in Example 2-22. Example 2-22. File Namespaces variables and constants using System using using using Table name constants private const String ORDERS_TABLE Orders private const String ORDERDETAILS_TABLE OrderDetails Relation name constants private const String ORDERS_ORDERDETAILS_RELATION Orders_OrderDetails_Relation Field name constants private const String ORDERID_FIELD OrderlD private const String CUSTOMERID_FIELD CustomerID private const String QUANTITY_FIELD Quantity . . . DataSet ds new DataSet Fill the Orders table and add it to the DataSet. SqlDataAdapter da new SqlDataAdapter SELECT FROM Orders Sql_ConnectString DataTable ordersTable new DataTable ORDERS_TABLE ordersTable . Add ordersTable Fill the OrderDetails table and add it to the DataSet. da new SqlDataAdapter SELECT FROM Order Details Sql_ConnectString DataTable orderDetailsTable new DataTable ORDERDETAILS_TABLE orderDetailsTable . Add orderDetailsTable Create a relation between the tables. ORDERS_ORDERDETAILS_RELATION ORDERID_FIELD orderDetailsTable. Columns ORDERID_FIELD true Add a column to Orders for the total items in all .