Skip to content

Latest commit

 

History

History
117 lines (75 loc) · 5.4 KB

File metadata and controls

117 lines (75 loc) · 5.4 KB

DataBinding Tutorial

Info

This is a walkthrough on using databinding in winforms and EF. It will guide you through setting up databinding and entity frameowork to allow data to be read and modified with very little code.

Steps

Setup Binding to Data

  1. Create a new VB Winforms Project -- https://github.com/VBDev2Dev/DataBinding/commit/68779b3a8d51402371d36b0b979f69aa373c135d

  2. Add class or classes that model your data -- https://github.com/VBDev2Dev/DataBinding/commit/77b92b9d5343bced53fd9d2fb7a22296c09b2c1e

    • Hit Ctrl + Shift + B to build project.
  3. Add a bound DataGridView to form -- https://github.com/VBDev2Dev/DataBinding/commit/ac7b15965d4d32672439c793b33b90646b568c00

    1. Add a DatagridView to the form
    2. Databind
      1. Set Datasource

        Set Datasource

      2. Pick object datasource

        Set Datasource type

      3. Select model class or classes

        Set Datasource class

    3. Edit Columns to move columns and set any needed properties
      1. Edit Columns

        Edit Columns

      2. Move Name column first

        Edit Columns Move

  4. Add code to bind the BindingSource to the data -- https://github.com/VBDev2Dev/DataBinding/commit/df5006ff5b48ff4b745a720f38e9c8c6071350c9

  5. Add a temporary button that will let you see the data has been updated.

    1. Add a button

    2. In click event add this code:

          MessageBox.Show($"You have {data.Count} items in the list.  The total of all C values is {data.Sum(function(r) r.C)}.")
  6. Debug the project

    1. Add some rows in grid
    2. Click button.

Switch To DB

  1. Add DB items to project

    1. Add Service Based Database. -- https://github.com/VBDev2Dev/DataBinding/commit/c035ce0b32bafeba0bb3cfeae4af943712564e6c This adds an empty sql server database that you can modify if needed

      Add DB

    2. Add EF Database Context -- https://github.com/VBDev2Dev/DataBinding/commit/4c7be23160b0a64244fccaa67c0c40d62f5fd606

      Add EF Context

    3. Adjust connection string of context to use DataDirectory Note that several ADO .net providers support this. Read the linked forum discussion to learn all about DataDirectory and how to set it. You will need to do so if you deploy the program and use a different folder than where the exe resides. -- https://github.com/VBDev2Dev/DataBinding/commit/46d5ef4b747f59e7a6d44466c6c8e662f9d555b4

    4. Enable Migrations This makes it so you are deliberate with your migrations. -- https://github.com/VBDev2Dev/DataBinding/commit/2fc00db8e0663a6a9110c2996e0cfa0e245520a8

      Enable Migrations

          Enable-Migrations
    5. Run Add-Migration and Update-Database -- https://github.com/VBDev2Dev/DataBinding/commit/717f17eb919a874b04a0bf188d4b3e1e896b0bae

      Add Migration Add Migration

      Add-Migration "Increase length of name"
      Update-Database
    6. Set database Copy to Output Directory property to Copy if newer This prevents builds from copying the database every time. This is needed if you want to add another migration. -- https://github.com/VBDev2Dev/DataBinding/commit/54581d486f79eb8cc5d03d7179b47fc3c3675c20

      Copy if newer

    7. Add a migration to change the length of the Name column Add-Migration and Update-Database -- https://github.com/VBDev2Dev/DataBinding/commit/2211fe3645e10e687a852689ef8c2fb9fa8d284e

          Add-Migration "Increase length of name"
          Update-Database
    8. Seed db with initial data -- https://github.com/VBDev2Dev/DataBinding/commit/613b45b60c15a78b4ae80744863f53c494fa8c10

          Update-Database
    9. Load data from db into context and show in grid -- https://github.com/VBDev2Dev/DataBinding/commit/e35c01ffb31207986e6357f56be700c1086cfd81

    10. Save changes to db -- https://github.com/VBDev2Dev/DataBinding/commit/aefe5fba1d8e1da7cc116d3b780d35af2bc7b43b

    11. Sort data coming from db -- https://github.com/VBDev2Dev/DataBinding/commit/8928f0daceb4767389cf008708c07be7d9423b7a

Make It Nice

  1. Make grid repsonsive -- https://github.com/VBDev2Dev/DataBinding/commit/409e37078c528e4ae093a1e321bf19d6dab42832

  2. Add logging -- https://github.com/VBDev2Dev/DataBinding/commit/fa2a49131ad3bc702e93b5736d53d644b472cc14

        Install-Package Serilog.Sinks.File
        Install-Package Serilog.Sinks.Console

    Add Logging

DON'T FORGET THE ERROR HANDLING!!!!!