Extending a Typed DataSet Using Partial ClassesAfter you have created your typed DataSet and configured all of your TableAdapters to run all of the queries that you need and annotated it so that all of the members have appropriate names, you still may find that you want to extend the functionality of your typed DataSet. For example, you may decide that you have several operations that will be performed frequently on the data, such as summing the total price of all orders within the DataSet. Rather than creating a separate utility class that takes a ConnectedOrderDataSet as a parameter, you can actually extend the class itself through the use of partial classes in C#. To do this, you can either right-click the DataSet in the Solution Explorer and choose View Code, or you can right-click the design surface of the DataSet designer and choose View Code. If this is the first time you have done this, you'll see an empty partial class implementation like the one that follows: namespace ConnectedDataSet { partial class ConnectedOrderDataSet { } } To add your own custom functionality, all you have to do is add the methods you want, such as the method shown in the following code: namespace ConnectedDataSet { partial class ConnectedOrderDataSet { public decimal SumAllOrders() { decimal totalValue = 0.0M; foreach (OrdersRow order in Orders) { foreach (OrderItemsRow orderItem in order.GetOrderItemsRows()) { totalValue += orderItem.Price; } } return totalValue; } } } With this method in place, you can call this method on the DataSet just like any other method: Console.WriteLine("Total sum of all purchases in DataSet: ${0:00.00}", orderData.SumAllOrders()); |