Export (0) Print
Expand All

Walkthrough: Connecting to Data in Objects 

This walkthrough shows how to create objects to hold customer and order data, and then create an object data source based on these objects. The object data source appears in the Data Sources window where items are dragged onto a form to create controls bound to the data in the object's public properties. The walkthrough also shows how to use TableAdapters to fetch data from the database and populate the objects.

The object data source is created by running the Data Source Configuration Wizard and selecting Object as the type of data source. After you complete the Data Source Configuration Wizard, the public properties of the object are available in the Data Sources Window for dragging onto your form.

NoteNote

You need to build the project that contains your object in order for it to appear in the Data Source Configuration Wizard. If your object is not available to the wizard, rebuild the project that contains the desired object(s).

Tasks illustrated in this walkthrough include:

  • Creating a new Windows Application project.

  • Creating sample objects to represent customers and orders.

  • Creating and configuring an object data source in your application based on the sample objects using the Data Source Configuration Wizard.

  • Adding controls to a form that are bound to the data in the custom objects.

  • Creating a dataset with TableAdapter's to move data between your objects and the database.

  • Editing a TableAdapter's main query.

  • Adding queries to a TableAdapter.

  • Populating your objects with data from the database.

To create the new Windows Application project

  1. From the File menu, create a New Project.

  2. Create a Windows Application named ObjectBindingWalkthrough and click OK. For more information, see Creating Windows-based Applications.

    The ObjectBindingWalkthrough project is created and added to Solution Explorer.

This walkthrough needs some objects to bind to, so the first step is to create some sample objects to represent customers and orders. To represent customers we will create a Customer object that represents a single customer. To represent orders we will create an Order object, which represents a single order, and an Orders object, which represents a collection of Order objects. For the collection of Customer objects we will use the built-in collection in the BindingSource class (explained later in this walkthrough).

To create the Customer object

  1. On the Project menu, click Add Class.

  2. Name the new class Customer and click Add.

  3. Replace the code in the Customer class file with the following code:

    NoteNote

    The Customer object contains an ordersCollection property of the type Orders. The editor will display a message stating Type 'Orders' is not defined. This message is expected and will disappear when you create the Order and Orders classes in the next section.

    namespace ObjectBindingWalkthrough
    {
        /// <summary>
        /// A single customer
        /// </summary>
        public class Customer
        {
            /// <summary>
            /// Creates a new customer
            /// </summary>
            public Customer()
            {
            }
    
            /// <summary>
            /// Creates a new customer
            /// </summary>
            /// <param name="customerID"></param>
            /// <param name="companyName"></param>
            /// <param name="contactName"></param>
            /// <param name="contactTitle"></param>
            /// <param name="address"></param>
            /// <param name="city"></param>
            /// <param name="region"></param>
            /// <param name="postalCode"></param>
            /// <param name="country"></param>
            /// <param name="phone"></param>
            /// <param name="fax"></param>
            public Customer(string customerID, string companyName,
               string contactName, string contactTitle,
               string address, string city, string region,
               string postalCode, string country,
               string phone, string fax)
            {
                customerIDValue = customerID;
            }
    
            private string customerIDValue;
            /// <summary>
            /// The ID that uniquely identifies this customer
            /// </summary>
            public string CustomerID
            {
                get { return customerIDValue; }
                set { customerIDValue = value; }
            }
    
            private string companyNameValue;
            /// <summary>
            /// The name for this customer
            /// </summary>
            public string CompanyName
            {
                get { return companyNameValue; }
                set { companyNameValue = value; }
            }
    
            private string contactNameValue;
            /// <summary>
            /// The name for this customer's contact
            /// </summary>
            public string ContactName
            {
                get { return contactNameValue; }
                set { contactNameValue = value; }
            }
    
            private string contactTitleValue;
            /// <summary>
            /// The title for this contact
            /// </summary>
            public string ContactTitle
            {
                get { return contactTitleValue; }
                set { contactTitleValue = value; }
            }
    
            private string addressValue;
            /// <summary>
            /// The address for this customer
            /// </summary>
            public string Address
            {
                get { return addressValue; }
                set { addressValue = value; }
            }
    
            private string cityValue;
            /// <summary>
            /// The city for this customer
            /// </summary>
            public string City
            {
                get { return cityValue; }
                set { cityValue = value; }
            }
    
            private string regionValue;
            /// <summary>
            /// The region for this customer
            /// </summary>
            public string Region
            {
                get { return regionValue; }
                set { regionValue = value; }
            }
    
            private string postalCodeValue;
            /// <summary>
            /// The postal code for this customer
            /// </summary>
            public string PostalCode
            {
                get { return postalCodeValue; }
                set { postalCodeValue = value; }
            }
    
            private string countryValue;
            /// <summary>
            /// The country for this customer
            /// </summary>
            public string Country
            {
                get { return countryValue; }
                set { countryValue = value; }
            }
    
            private string phoneValue;
            /// <summary>
            /// The phone number for this customer
            /// </summary>
            public string Phone
            {
                get { return phoneValue; }
                set { phoneValue = value; }
            }
    
            private string faxValue;
            /// <summary>
            /// The fax number for this customer
            /// </summary>
            public string Fax
            {
                get { return faxValue; }
                set { faxValue = value; }
            }
    
            private System.ComponentModel.BindingList<Order> ordersCollection = 
                new System.ComponentModel.BindingList<Order>();
    
            public System.ComponentModel.BindingList<Order> Orders
            {
                get { return ordersCollection; }
                set { ordersCollection = value; }
            }
    
            public override string ToString()
            {
                return this.CompanyName + " (" + this.CustomerID + ")";
            }
        }
    }
    
    
    package ObjectBindingWalkthrough;
    
    /// <summary>
    /// A single customer
    /// </summary>
    public class Customer
    {
        /// <summary>
        /// Creates a new customer
        /// </summary>
        public Customer()
        {
        }
    
        /// <summary>
        /// Creates a new customer
        /// </summary>
        /// <param name="customerID"></param>
        /// <param name="companyName"></param>
        /// <param name="contactName"></param>
        /// <param name="contactTitle"></param>
        /// <param name="address"></param>
        /// <param name="city"></param>
        /// <param name="region"></param>
        /// <param name="postalCode"></param>
        /// <param name="country"></param>
        /// <param name="phone"></param>
        /// <param name="fax"></param>
        public Customer(String customerID, String companyName,
            String contactName, String contactTitle,
            String address, String city, String region,
            String postalCode, String country,
            String phone, String fax)
        {
            customerIDValue = customerID;
        }
    
        private String customerIDValue;
        /// <summary>
        /// The ID that uniquely identifies this customer
        /// </summary>
        public String get_CustomerID()
        {
            return customerIDValue;
        }
        public void set_CustomerID(String value)
        {
            customerIDValue = value;
        }
    
        private String companyNameValue;
        /// <summary>
        /// The name for this customer
        /// </summary>
        public String get_CompanyName()
        {
            return companyNameValue;
        }
        public void set_CompanyName(String value)
        {
            companyNameValue = value;
        }
    
        private String contactNameValue;
        /// <summary>
        /// The name for this customer's contact
        /// </summary>
        public String get_ContactName()
        {
            return contactNameValue;
        }
        public void set_ContactName(String value)
        {
            contactNameValue = value;
        }
    
        private String contactTitleValue;
        /// <summary>
        /// The title for this contact
        /// </summary>
        public String get_ContactTitle()
        {
            return contactTitleValue;
        }
        public void set_ContactTitle(String value)
        {
            contactTitleValue = value;
        }
    
        private String addressValue;
        /// <summary>
        /// The address for this customer
        /// </summary>
        public String get_Address()
        {
            return addressValue;
        }
        public void set_Address(String value)
        {
            addressValue = value;
        }
    
        private String cityValue;
        /// <summary>
        /// The city for this customer
        /// </summary>
        public String get_City()
        {
            return cityValue;
        }
        public void set_City(String value)
        {
            cityValue = value;
        }
    
        private String regionValue;
        /// <summary>
        /// The region for this customer
        /// </summary>
        public String get_Region()
        {
            return regionValue;
        }
        public void set_Region(String value)
        {
            regionValue = value;
        }
    
        private String postalCodeValue;
        /// <summary>
        /// The postal code for this customer
        /// </summary>
        public String get_PostalCode()
        {
            return postalCodeValue;
        }
        public void set_PostalCode(String value)
        {
            postalCodeValue = value;
        }
    
        private String countryValue;
        /// <summary>
        /// The country for this customer
        /// </summary>
        public String get_Country()
        {
            return countryValue;
        }
        public void set_Country(String value)
        {
            countryValue = value;
        }
    
        private String phoneValue;
        /// <summary>
        /// The phone number for this customer
        /// </summary>
        public String get_Phone()
        {
            return phoneValue;
        }
        public void set_Phone(String value)
        {
            phoneValue = value;
        }
    
        private String faxValue;
        /// <summary>
        /// The fax number for this customer
        /// </summary>
        public String get_Fax()
        {
            return faxValue;
        }
        public void set_Fax(String value)
        {
            faxValue = value;
        }
    
        private System.ComponentModel.BindingList<Order> ordersCollection = 
            new System.ComponentModel.BindingList<Order>();
    
        public System.ComponentModel.BindingList<Order> get_Orders()
        {
            return ordersCollection;
        }
        public void set_Orders(System.ComponentModel.BindingList<Order> value)
        {
            ordersCollection = value;
        }
    
        public String ToString()
        {
            return this.get_CompanyName() + " (" + this.get_CustomerID() + ")";
        }
    }
    
    

To create the Order object and Orders collection

  1. On the Project menu, select Add Class.

  2. Name the new class Order and click Add.

  3. Replace the code in the Order class file with the following code:

    using System;
    
    namespace ObjectBindingWalkthrough
    {
       /// <summary>
       /// A single order
       /// </summary>
       public class Order
       {
          /// <summary>
          /// Creates a new order
          /// </summary>
          public Order()
          {
          }
    
          /// <summary>
          /// Creates a new order
          /// </summary>
          /// <param name="orderid"></param>
          /// <param name="customerID"></param>
          /// <param name="employeeID"></param>
          /// <param name="orderDate"></param>
          /// <param name="requiredDate"></param>
          /// <param name="shippedDate"></param>
          /// <param name="shipVia"></param>
          /// <param name="freight"></param>
          /// <param name="shipName"></param>
          /// <param name="shipAddress"></param>
          /// <param name="shipCity"></param>
          /// <param name="shipRegion"></param>
          /// <param name="shipPostalCode"></param>
          /// <param name="shipCountry"></param>
          public Order(int orderid, string customerID,
             int employeeID, DateTime orderDate,
             DateTime requiredDate, DateTime shippedDate,
             int shipVia, decimal freight,
             string shipName, string shipAddress,
             string shipCity, string shipRegion,
             string shipPostalCode, string shipCountry)
          {
             
          }
    
          private int orderIDValue;
          /// <summary>
          /// The ID that uniquely identifies this order
          /// </summary>
          public int OrderID
          {
             get { return orderIDValue; }
             set { orderIDValue = value; }
          }
          
          private string customerIDValue;
          /// <summary>
          /// The customer who placed this order
          /// </summary>
          public string CustomerID
          {
             get { return customerIDValue; }
             set { customerIDValue = value; }
          }
          
          private int employeeIDValue;
          /// <summary>
          /// The ID of the employee who took this order
          /// </summary>
          public int EmployeeID
          {
             get { return employeeIDValue; }
             set { employeeIDValue = value; }
          }
          
          private DateTime orderDateValue;
          /// <summary>
          /// The date this order was placed
          /// </summary>
          public DateTime OrderDate
          {
             get { return orderDateValue; }
             set { orderDateValue = value; }
          }
          
          private DateTime requiredDateValue;
          /// <summary>
          /// The date this order is required
          /// </summary>
          public DateTime RequiredDate
          {
             get { return requiredDateValue; }
             set { requiredDateValue = value; }
          }
          
          private DateTime shippedDateValue;
          /// <summary>
          /// The date this order was shipped
          /// </summary>
          public DateTime ShippedDate
          {
             get { return shippedDateValue; }
             set { shippedDateValue = value; }
          }
          
          private int shipViaValue;
          /// <summary>
          /// The shipping method of this order
          /// </summary>
          public int ShipVia
          {
             get { return shipViaValue; }
             set { shipViaValue = value; }
          }
          
          private decimal freightValue;
          /// <summary>
          /// The freight charge for this order
          /// </summary>
          public decimal Freight
          {
             get { return freightValue; }
             set { freightValue = value; }
          }
          
          private string shipNameValue;
          /// <summary>
          /// The name of the recipient for this order
          /// </summary>
          public string ShipName
          {
             get { return shipNameValue; }
             set { shipNameValue = value; }
          }
          
          private string shipAddressValue;
          /// <summary>
          /// The address to ship this order to
          /// </summary>
          public string ShipAddress
          {
             get { return shipAddressValue; }
             set { shipAddressValue = value; }
          }
          
          private string shipCityValue;
          /// <summary>
          /// The city to ship this order to
          /// </summary>
          public string ShipCity
          {
             get { return shipCityValue; }
             set { shipCityValue = value; }
          }
          
          private string shipRegionValue;
          /// <summary>
          /// The region to ship this order to
          /// </summary>
          public string ShipRegion
          {
             get { return shipRegionValue; }
             set { shipRegionValue = value; }
          }
          
          private string shipPostalCodeValue;
          /// <summary>
          /// The postal code to ship this order to
          /// </summary>
          public string ShipPostalCode
          {
             get { return shipPostalCodeValue; }
             set { shipPostalCodeValue = value; }
          }
          
          private string shipCountryValue;
          /// <summary>
          /// The country to ship this order to
          /// </summary>
          public string ShipCountry
          {
             get { return shipCountryValue; }
             set { shipCountryValue = value; }
          }
          
       }
    
    
       /// <summary>
       /// A collection of Order objects
       /// </summary>
       class Orders: System.ComponentModel.BindingList<Order>
       {
       
       }
    }
    
    
    package ObjectBindingWalkthrough;
    
    import System.*;
    
    /// <summary>
    /// A single order
    /// </summary>
    public class Order
    {
        /// <summary>
        /// Creates a new order
        /// </summary>
        public Order()
        {
        }
    
        /// <summary>
        /// Creates a new order
        /// </summary>
        /// <param name="orderid"></param>
        /// <param name="customerID"></param>
        /// <param name="employeeID"></param>
        /// <param name="orderDate"></param>
        /// <param name="requiredDate"></param>
        /// <param name="shippedDate"></param>
        /// <param name="shipVia"></param>
        /// <param name="freight"></param>
        /// <param name="shipName"></param>
        /// <param name="shipAddress"></param>
        /// <param name="shipCity"></param>
        /// <param name="shipRegion"></param>
        /// <param name="shipPostalCode"></param>
        /// <param name="shipCountry"></param>
        public Order(int orderid, String customerID,
            int employeeID, DateTime orderDate,
            DateTime requiredDate, DateTime shippedDate,
            int shipVia, System.Decimal freight,
            String shipName, String shipAddress,
            String shipCity, String shipRegion,
            String shipPostalCode, String shipCountry)
        {
            
        }
    
        private int orderIDValue;
        /// <summary>
        /// The ID that uniquely identifies this order
        /// </summary>
        public int get_OrderID()
        {
            return orderIDValue;
        }
        public void set_OrderID(int value)
        {
            orderIDValue = value;
        }
        
        private String customerIDValue;
        /// <summary>
        /// The customer who placed this order
        /// </summary>
        public String get_CustomerID()
        {
            return customerIDValue;
        }
        public void set_CustomerID(String value)
        {
            customerIDValue = value;
        }
        
        private int employeeIDValue;
        /// <summary>
        /// The ID of the employee who took this order
        /// </summary>
        public int get_EmployeeID()
        {
            return employeeIDValue;
        }
        public void set_EmployeeID(int value)
        {
            employeeIDValue = value;
        }
        
        private DateTime orderDateValue;
        /// <summary>
        /// The date this order was placed
        /// </summary>
        public DateTime get_OrderDate()
        {
            return orderDateValue;
        }
        public void set_OrderDate(DateTime value)
        {
            orderDateValue = value;
        }
        
        private DateTime requiredDateValue;
        /// <summary>
        /// The date this order is required
        /// </summary>
        public DateTime get_RequiredDate()
        {
            return requiredDateValue;
        }
        public void set_RequiredDate(DateTime value)
        {
            requiredDateValue = value;
        }
        
        private DateTime shippedDateValue;
        /// <summary>
        /// The date this order was shipped
        /// </summary>
        public DateTime get_ShippedDate()
        {
            return shippedDateValue;
        }
        public void set_ShippedDate(DateTime value)
        {
            shippedDateValue = value;
        }
        
        private int shipViaValue;
        /// <summary>
        /// The shipping method of this order
        /// </summary>
        public int get_ShipVia()
        {
            return shipViaValue;
        }
        public void set_ShipVia(int value)
        {
            shipViaValue = value;
        }
        
        private System.Decimal freightValue;
        /// <summary>
        /// The freight charge for this order
        /// </summary>
        public System.Decimal get_Freight()
        {
            return freightValue;
        }
        public void set_Freight(System.Decimal value)
        {
            freightValue = value;
        }
        
        private String shipNameValue;
        /// <summary>
        /// The name of the recipient for this order
        /// </summary>
        public String get_ShipName()
        {
            return shipNameValue;
        }
        public void set_ShipName(String value)
        {
            shipNameValue = value;
        }
        
        private String shipAddressValue;
        /// <summary>
        /// The address to ship this order to
        /// </summary>
        public String get_ShipAddress()
        {
            return shipAddressValue;
        }
        public void set_ShipAddress(String value)
        {
            shipAddressValue = value;
        }
        
        private String shipCityValue;
        /// <summary>
        /// The city to ship this order to
        /// </summary>
        public String get_ShipCity()
        {
            return shipCityValue;
        }
        public void set_ShipCity(String value)
        {
            shipCityValue = value;
        }
        
        private String shipRegionValue;
        /// <summary>
        /// The region to ship this order to
        /// </summary>
        public String get_ShipRegion()
        {
            return shipRegionValue;
        }
        public void set_ShipRegion(String value)
        {
            shipRegionValue = value;
        }
        
        private String shipPostalCodeValue;
        /// <summary>
        /// The postal code to ship this order to
        /// </summary>
        public String get_ShipPostalCode()
        {
            return shipPostalCodeValue;
        }
        public void set_ShipPostalCode(String value)
        {
            shipPostalCodeValue = value;
        }
        
        private String shipCountryValue;
        /// <summary>
        /// The country to ship this order to
        /// </summary>
        public String get_ShipCountry()
        {
            return shipCountryValue;
        }
        public void set_ShipCountry(String value)
        {
            shipCountryValue = value;
        }
        
    }
    
    /// <summary>
    /// A collection of Order objects
    /// </summary>
    class Orders extends System.ComponentModel.BindingList<Order>
    {
    
    }
    
    
  4. From the File menu, choose Save All.

You can create a data source based on the objects created in the previous step by running the Data Source Configuration Wizard.

To create the object data source

  1. Open the Data Sources window by clicking the Data menu and choosing Show Data Sources.

  2. Click Add New Data Source in the Data Sources window.

  3. Select Object on the Choose a Data Source Type page, and click Next.

  4. Expand the ObjectBindingWalkthrough nodes and select the Customer object.

    NoteNote

    If the Customer object is not available, click Cancel, then select Build ObjectBindingWalkthrough from the Build menu. After successfully building the project, restart the wizard (step 2) and the custom objects will appear.

  5. Click Finish.

    The Customer object appears in the Data Sources window.

Controls bound to the Customer object are created by dragging items from the Data Sources window onto a form.

To create a form with controls bound to the object properties

  1. In Solution Explorer, select Form1, and click View Designer.

  2. Drag the Customer node from the Data Sources window onto Form1.

  3. Expand the Customer node and drag the Orders node from the Data Sources window onto Form1.

To move data between the objects and the database, we are going to use TableAdapters. You can create TableAdapters for the Customers and Orders tables using the Data Source Configuration Wizard.

To create the TableAdapters

  1. From the Data menu, choose Add New Data Source.

  2. Select Database on the Choose a Data Source Type page.

  3. On the Choose your Data Connection page, do one of the following:

  4. Click Next on the Save connection string to the Application Configuration file page.

  5. Expand the Tables node on the Choose your Database Objects page.

  6. Select the Customers and Orders tables, and then click Finish.

    The NorthwindDataSet is added to your project and the Customers and Orders tables appear in the Data Sources window under the NorthwindDataSet node.

You can add instances of the CustomersTableAdapter, OrdersTableAdapter, and NorthwindDataSet to the form by dragging their representative components from the Toolbox.

To fill the Customer objects with data from the Customers table

  1. From the Build menu, choose Build Solution.

  2. Drag a NorthwindDataSet from the Toolbox onto Form1.

  3. Drag a CustomersTableAdapter from the Toolbox onto Form1.

  4. Drag an OrdersTableAdapter from the Toolbox onto Form1.

In real-world applications, you will likely never return an entire table of data. For this walkthrough we will return the top five customers.

NoteNote

You would typically pass in a parameter to select which customers you want to return, but for brevity in this walkthrough we will hard-code the query to return only five customers and eliminate the need of creating a user interface for inputting parameter values.

To add an additional query to the CustomersTableAdapter

  1. In Solution Explorer, double-click the NorthwindDataSet.xsd file.

    The NorthwindDataSet opens in the Dataset Designer.

  2. Right-click the CustomersTableAdapter and select Add Query.

    The TableAdapter Query Configuration Wizard opens.

  3. Leave the default of Use SQL statements and click Next.

  4. Leave the default of SELECT which returns rows and click Next.

  5. Replace the SQL statement with the following and click Next:

    SELECT Top 5 CustomerID, CompanyName, ContactName, ContactTitle, Address, 
    City, Region, PostalCode, Country, Phone, Fax 
    FROM Customers 
    
  6. Clear the Fill a DataTable check box.

  7. Name the Return a DataTable method GetTop5Customers and click Finish.

    The GetTop5Customers query is added to the CustomersTableAdapter.

When fetching orders from the database, we do not want to return the entire table of orders; we only want the orders for a specific customer. The following procedure details how to reconfigure a TableAdapter with a new query (as opposed to adding an additional query as we did to the CustomersTableAdapter in the previous step).

To reconfigure the TableAdapter's main query to return a single customer's orders

  1. Right-click the OrdersTableAdapter and choose Configure.

    The TableAdapter Query Configuration Wizard opens.

  2. Replace the SQL statement with the following and click Next:

    SELECT OrderID, CustomerID, EmployeeID, OrderDate, 
    RequiredDate, ShippedDate, ShipVia, Freight, 
    ShipName, ShipAddress, ShipCity, ShipRegion, 
    ShipPostalCode, ShipCountry 
    FROM Orders 
    WHERE CustomerID = @CustomerID
    
  3. Clear the Fill a DataTable check box.

  4. Name the Return a DataTable method GetDataByCustomerID and click Finish.

    The OrdersTableAdapter's main Fill query is replaced with the GetDataByCustomerID query.

  5. Build the project by choosing Build Solution from the Build menu.

To load data into our custom objects, you execute the TableAdapter queries that return new data tables (as opposed to TableAdapter queries that fill existing data tables). The code then loops through the table and populates each Customer object with the customer information, as well as populating all orders in each Customer.Orders collection. Notice how each Customer object is added to the CustomerBindingSource's internal collection (CustomerBindingSource.Add(currentCustomer)); that is, the BindingSource provides a built-in strongly typed collection of Customers accessible through the List property.

To load the objects with data

  1. In Solution Explorer, select Form1, and click View Code.

  2. Replace the code in Form1 with the following code:

    using System;
    using System.Windows.Forms;
    
    namespace ObjectBindingWalkthrough
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                this.Load += Form1_Load;
            }
    
            private void LoadCustomers()
            {
                NorthwindDataSet.CustomersDataTable customerData = 
                    customersTableAdapter1.GetTop5Customers();
                
                foreach (NorthwindDataSet.CustomersRow customerRow in customerData)
                {
                    Customer currentCustomer = new Customer();
                    currentCustomer.CustomerID = customerRow.CustomerID;
                    currentCustomer.CompanyName = customerRow.CompanyName;
    
                    if (customerRow.IsAddressNull() == false)
                    {
                        currentCustomer.Address = customerRow.Address;
                    }
    
                    if (customerRow.IsCityNull() == false)
                    {
                        currentCustomer.City = customerRow.City;
                    }
    
                    if (customerRow.IsContactNameNull() == false)
                    {
                        currentCustomer.ContactName = customerRow.ContactName;
                    }
    
                    if (customerRow.IsContactTitleNull() == false)
                    {
                        currentCustomer.ContactTitle = customerRow.ContactTitle;
                    }
    
                    if (customerRow.IsCountryNull() == false)
                    {
                        currentCustomer.Country = customerRow.Country;
                    }
    
                    if (customerRow.IsFaxNull() == false)
                    {
                        currentCustomer.Fax = customerRow.Fax;
                    }
    
                    if (customerRow.IsPhoneNull() == false)
                    {
                        currentCustomer.Phone = customerRow.Phone;
                    }
    
                    if (customerRow.IsPostalCodeNull() == false)
                    {
                        currentCustomer.PostalCode = customerRow.PostalCode;
                    }
    
                    if (customerRow.IsRegionNull() == false)
                    {
                        currentCustomer.Region = customerRow.Region;
                    }
    
                    LoadOrders(currentCustomer);
                    customerBindingSource.Add(currentCustomer);
                }
            }
    
    
            private void LoadOrders(Customer currentCustomer)
            {
                NorthwindDataSet.OrdersDataTable orderData = 
                    ordersTableAdapter1.GetDataByCustomerID(currentCustomer.CustomerID);
    
                foreach (NorthwindDataSet.OrdersRow orderRow in orderData)
                {
                    Order currentOrder = new Order();
                    currentOrder.OrderID = orderRow.OrderID;
    
                    if (orderRow.IsCustomerIDNull() == false)
                    {
                        currentOrder.CustomerID = orderRow.CustomerID;
                    }
    
                    if (orderRow.IsEmployeeIDNull() == false)
                    {
                        currentOrder.EmployeeID = orderRow.EmployeeID;
                    }
    
                    if (orderRow.IsFreightNull() == false)
                    {
                        currentOrder.Freight = orderRow.Freight;
                    }
    
                    if (orderRow.IsOrderDateNull() == false)
                    {
                        currentOrder.OrderDate = orderRow.OrderDate;
                    }
    
                    if (orderRow.IsRequiredDateNull() == false)
                    {
                        currentOrder.RequiredDate = orderRow.RequiredDate;
                    }
    
                    if (orderRow.IsShipAddressNull() == false)
                    {
                        currentOrder.ShipAddress = orderRow.ShipAddress;
                    }
    
                    if (orderRow.IsShipCityNull() == false)
                    {
                        currentOrder.ShipCity = orderRow.ShipCity;
                    }
    
                    if (orderRow.IsShipCountryNull() == false)
                    {
                        currentOrder.ShipCountry = orderRow.ShipCountry;
                    }
    
                    if (orderRow.IsShipNameNull() == false)
                    {
                        currentOrder.ShipName = orderRow.ShipName;
                    }
    
                    if (orderRow.IsShippedDateNull() == false)
                    {
                        currentOrder.ShippedDate = orderRow.ShippedDate;
                    }
    
                    if (orderRow.IsShipPostalCodeNull() == false)
                    {
                        currentOrder.ShipPostalCode = orderRow.ShipPostalCode;
                    }
    
                    if (orderRow.IsShipRegionNull() == false)
                    {
                        currentOrder.ShipRegion = orderRow.ShipRegion;
                    }
    
                    if (orderRow.IsShipViaNull() == false)
                    {
                        currentOrder.ShipVia = orderRow.ShipVia;
                    }
                    currentCustomer.Orders.Add(currentOrder);
                }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                LoadCustomers();
            }
    
        }
    }
    
    
    package ObjectBindingWalkthrough;
    
    import System.Windows.Forms.*;
    
    public class Form1 extends System.Windows.Forms.Form
    {
        private void LoadCustomers() throws System.Data.StrongTypingException
        {
            NorthwindDataSet.CustomersDataTable customerData = 
                northwindDataSetCustomersTableAdapter1.GetTop5Customers();
            
            for (int i=0; i < customerData.get_Rows().get_Count(); i++)
            {
                NorthwindDataSet.CustomersRow customerRow = (NorthwindDataSet.CustomersRow)
                    customerData.get_Rows().get_Item(i);
    
                Customer currentCustomer = new Customer();
                currentCustomer.set_CustomerID(customerRow.get_CustomerID());
                currentCustomer.set_CompanyName(customerRow.get_CompanyName());
    
                if (customerRow.IsAddressNull() == false)
                {
                    currentCustomer.set_Address(customerRow.get_Address());
                }
    
                if (customerRow.IsCityNull() == false)
                {
                    currentCustomer.set_City(customerRow.get_City());
                }
    
                if (customerRow.IsContactNameNull() == false)
                {
                    currentCustomer.set_ContactName(customerRow.get_ContactName());
                }
    
                if (customerRow.IsContactTitleNull() == false)
                {
                    currentCustomer.set_ContactTitle(customerRow.get_ContactTitle());
                }
    
                if (customerRow.IsCountryNull() == false)
                {
                    currentCustomer.set_Country(customerRow.get_Country());
                }
    
                if (customerRow.IsFaxNull() == false)
                {
                    currentCustomer.set_Fax(customerRow.get_Fax());
                }
    
                if (customerRow.IsPhoneNull() == false)
                {
                    currentCustomer.set_Phone(customerRow.get_Phone());
                }
    
                if (customerRow.IsPostalCodeNull() == false)
                {
                    currentCustomer.set_PostalCode(customerRow.get_PostalCode());
                }
    
                if (customerRow.IsRegionNull() == false)
                {
                    currentCustomer.set_Region(customerRow.get_Region());
                }
    
                LoadOrders(currentCustomer);
                customersBindingSource.Add(currentCustomer);
            }
        }
    
    
        private void LoadOrders(Customer currentCustomer) throws System.Data.StrongTypingException
        {
            NorthwindDataSet.OrdersDataTable orderData = 
                northwindDataSetOrdersTableAdapter1.GetDataByCustomerID(currentCustomer.get_CustomerID());
    
            for (int i=0; i < orderData.get_Rows().get_Count(); i++)
            {
                NorthwindDataSet.OrdersRow orderRow = (NorthwindDataSet.OrdersRow)
                    orderData.get_Rows().get_Item(i);
    
                Order currentOrder = new Order();
                currentOrder.set_OrderID(orderRow.get_OrderID());
    
                if (orderRow.IsCustomerIDNull() == false)
                {
                    currentOrder.set_CustomerID(orderRow.get_CustomerID());
                }
    
                if (orderRow.IsEmployeeIDNull() == false)
                {
                    currentOrder.set_EmployeeID(orderRow.get_EmployeeID());
                }
    
                if (orderRow.IsFreightNull() == false)
                {
                    currentOrder.set_Freight(orderRow.get_Freight());
                }
    
                if (orderRow.IsOrderDateNull() == false)
                {
                    currentOrder.set_OrderDate(orderRow.get_OrderDate());
                }
    
                if (orderRow.IsRequiredDateNull() == false)
                {
                    currentOrder.set_RequiredDate(orderRow.get_RequiredDate());
                }
    
                if (orderRow.IsShipAddressNull() == false)
                {
                    currentOrder.set_ShipAddress(orderRow.get_ShipAddress());
                }
    
                if (orderRow.IsShipCityNull() == false)
                {
                    currentOrder.set_ShipCity(orderRow.get_ShipCity());
                }
    
                if (orderRow.IsShipCountryNull() == false)
                {
                    currentOrder.set_ShipCountry(orderRow.get_ShipCountry());
                }
    
                if (orderRow.IsShipNameNull() == false)
                {
                    currentOrder.set_ShipName(orderRow.get_ShipName());
                }
    
                if (orderRow.IsShippedDateNull() == false)
                {
                    currentOrder.set_ShippedDate(orderRow.get_ShippedDate());
                }
    
                if (orderRow.IsShipPostalCodeNull() == false)
                {
                    currentOrder.set_ShipPostalCode(orderRow.get_ShipPostalCode());
                }
    
                if (orderRow.IsShipRegionNull() == false)
                {
                    currentOrder.set_ShipRegion(orderRow.get_ShipRegion());
                }
    
                if (orderRow.IsShipViaNull() == false)
                {
                    currentOrder.set_ShipVia(orderRow.get_ShipVia());
                }
                currentCustomer.get_Orders().Add(currentOrder);
            }
        }
    
    
        private void Form1_Load(Object sender, System.EventArgs e) throws System.Data.StrongTypingException
        {
            LoadCustomers();
        }
    
    
        // REST OF CLASS FORM1 HERE...
    
    

To test the application

  1. Press F5 to run the application.

  2. The form opens and the DataGridView controls are populated with the sample data.

  3. Navigate the customers in the DataGridView to display their associated orders.

To add functionality to your application

Community Additions

ADD
Show:
© 2014 Microsoft