Sorry, there are no translations available at the moment.

Ajout et suppression

Overview

This application builds upon the completed solution from the previous chapter "Hello World!".

Added herein is support for adding and deleting entities.

Buttons on a BindingNavigator are used as the mechanism for performing an add or delete of an Employee.

For Orders, which are displayed in grids, the NewItemRow at the bottom of a .NET DataGridView is used for adding new entities.

To delete an Order, you select its row in its grid and press the DEL key on your keyboard.

Step 1

  1. Download the completed solution : available here

  2. Uncompress the file your just downloaded.

  3. Open the "DevForce01.sln" solution file with Visual Studio 2008

  4. Set the "WinForms01" project as the startup project :

    • Right-click on the "WinForms01" project in the solution explorer
    • Click on "Set as StartUp Project"


  5. Refresh the connection string for the two ".edmx" files :

    • Double-click on "ServerModelAw2000.edmx"
    • Right-click on an empty white area of the schema
    • Click on "Update Model from Database"
    • Select the data connection from the drop-down list ("name of your machine\ sqlexpress.AdventureWorkds2000.dbo"). If it is not available from the list, create it with the "New Connection" button)
    • Click on "Next" then "Finish"
    • Double-click on "ServerModelNorthwindIB.edmx"
    • Right-click on an empty white area of the schema
    • Click on "Update Model from Database"
    • Select the data connection from the drop-down list ("name of your machine\ sqlexpress.NorthwindIB.dbo"). If it is not available from the list, create it with the "New Connection" button)
    • Click on "Next" then "Finish"
    • Close the two ".edmx" files. Both times, you will be asked the following :
      An edit that affects the '[...]\DomainModel\DomainModel.ibedmx' model has occurred.
      Do you want to regenerate the model?
      ---------------------------
      Yes No
      ---------------------------
      Answer "Yes" each time.


  6. Verify that the references of the "DevForce01" project are correctly loaded. If not, you will see a yellow icon, and you'll need to remove the corresponding references and add them again :

    • IdeaBlade.UI
    • IdeaBlade.UI.WinForms
    • IdeaBlade.UI.WinForms.DotnetControls
    • IdeaBlade.Util


  7. Rebuild the solution. You will be asked the following :

    ---------------------------
    Update configuration file
    ---------------------------
    The 'ideablade.configuration' section of the 'app.config' file in the 'Console01' project is no longer the same as that of its domain model.

    Do you want to update this file to stay in sync?
    ---------------------------
    Yes No
    ---------------------------

    Answer "Yes". And when asked :

    ---------------------------
    Update configuration file
    ---------------------------
    The 'ideablade.configuration' section of the 'app.config' file in the 'WinForms01' project is no longer the same as that of its domain model.

    Do you want to update this file to stay in sync?
    ---------------------------
    Yes No
    ---------------------------

    Answer "Yes" again.

Step 2 : Adding and deleting with the binding navigator

  1. Add the following code to the "Employee.cs"

     
        #region Create Method
     
        public static Employee Create(EntityManager manager, string lastName, string firstName) {
          Employee anEmployee = manager.CreateEntity<Employee>();
          anEmployee.EntityAspect.AddToManager();
          anEmployee.FirstName = firstName;
          anEmployee.LastName = lastName;
          return anEmployee;
        }
     
        #endregion Create Method
     
  2. Add the following code to the "EmployeeForm.cs"

     
            #region Add-Delete Handlers
     
            private void __employeesBindingNavigatorAddNewItemButton_Click(object sender, EventArgs e)
            {
                AddNewEmployee();
            }
     
            private void __employeesBindingNavigatorDeleteItemButton_Click(object sender, EventArgs e)
            {
                DeleteEmployee();
            }
     
            private void AddNewEmployee()
            {
                Employee newEmployee = Employee.Create(_manager, "[Last Name]", "[First Name]");
                _employees.Add(newEmployee);
                _employeesBindingSource.MoveLast();
            }
     
            private void DeleteEmployee()
            {
                CurrentEmployee.EntityAspect.Delete();
                _employeesBindingSource.RemoveCurrent();
            }
     
            #endregion Add-Delete Handlers
     
            #region Public Properties
            public Employee CurrentEmployee
            {
                get
                {
                    if (_employeesBindingSource.Current == null)
                    {
                        return _manager.GetNullEntity<Employee>();
                    }
                    return (_employeesBindingSource.Current as Employee);
                }
            }
            #endregion Public Properties
     

    Add the following code to the "EmployeeForm.cs", in the ConfigureBindingNavigators() function:

     
                // Disable default functionality of the Add and Delete buttons
                // on the BindingNavigator. We'll take control of it when ready.
                _employeesBindingNavigator.AddNewItem = null;
                _employeesBindingNavigator.DeleteItem = null;
                _employeesBindingNavigatorAddNewItemButton.Enabled = true;
                _employeesBindingNavigatorDeleteItemButton.Enabled = false;
     
  3. Open the "EmployeeForm.cs" in designer mode.

    Select the "Add Item" button from the binding navigator. Rename it to "_employeesBindingNavigatorAddNewItemButton" and set the click event to "__employeesBindingNavigatorAddNewItemButton_Click"

    Select the "Delete" button from the binding navigator. Rename it to "_employeesBindingNavigatorDeleteItemButton" and set the click event to "__employeesBindingNavigatorDeleteItemButton_Click"

  4. Add the following code to the "ConfigureBindingSources()" function.

     
               _employeesBindingSource.ListChanged += new ListChangedEventHandler(_employeesBindingSource_ListChanged);
     

    Then add the following function :

     
            void _employeesBindingSource_ListChanged(object sender, ListChangedEventArgs e)
            {
                _employeesBindingNavigatorDeleteItemButton.Enabled = (sender as BindingSource).Count > 0;
            }
     

Demo solution, with an aditionnal Form to manage the Products :

Available here