7.12. Binding a Windows DataGrid to Master-Detail Data
Problem
You need to bind both a parent table
and child table within a
DataSet to a DataGrid so that
the child data is displayed when the parent is expanded, and update
the database with edits made to data in both tables.
Solution
Use the approach demonstrated in the sample code.
The sample uses eight stored procedures, which are shown in Example 7-20 through Example 7-27:
GetOrdersUsed to retrieve a single record from the Orders table if the optional
@OrderIdparameter is specified or all Orders records if it is notDeleteOrdersUsed to delete the record specified by the
@OrderIdparameter from the Orders tableInsertOrdersUsed to insert a record into the Orders table and return the OrderID identity value for the new record
UpdateOrdersUsed to update all field values for the record in the Orders table specified by the
@OrderIdinput parameterGetOrderDetailsUsed to retrieve a single record from the Order Details table if the optional
@OrderIdand@ProductIDparameters are specified, or all Order Details records if it is notDeleteOrderDetailsUsed to delete the record specified by the
@OrderIdand@ProductIDparameters from the Order Details tableInsertOrderDetailsUsed to insert a record into the Order Details table
UpdateOrderDetailsUsed to update all field values for the record in the Order Details table specified by the
@OrderIdand@ProductIDinput parameters
Example 7-20. Stored procedure: GetOrders
CREATE PROCEDURE GetOrders @OrderID ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access