wingman007

ADO.NETEntityFrameworkQuickStart

Oct 26th, 2015
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.98 KB | None | 0 0
  1. // the tutorial at https://msdn.microsoft.com/en-us/library/vstudio/bb399568(v=vs.100).aspx
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using System.Data.Objects;
  12. using System.Data.Objects.DataClasses;
  13.  
  14. namespace CourseManager
  15. {
  16.     public partial class CourseViewer : Form
  17.     {
  18.         //Create an ObjectContext instance based on SchoolEntity
  19.         private SchoolEntities schoolContext;
  20.         public CourseViewer()
  21.         {
  22.             InitializeComponent();
  23.         }
  24.  
  25.         private void closeForm_Click(object sender, EventArgs e)
  26.         {
  27.             //Dispose the object context.
  28.             schoolContext.Dispose();
  29.  
  30.             this.Close();
  31.         }
  32.  
  33.         private void CourseViewer_Load(object sender, EventArgs e)
  34.         {
  35.             //Initialize the ObjectContext
  36.             schoolContext = new SchoolEntities();
  37.  
  38.             // Define a query that returns all Department  
  39.             // objects and course objects, ordered by name.
  40.             var departmentQuery = from d in schoolContext.Departments.Include("Courses")
  41.                                   orderby d.Name
  42.                                   select d;
  43.             try
  44.             {
  45.                 // Bind the ComboBox control to the query,
  46.                 // which is executed during data binding.
  47.                 // To prevent the query from being executed multiple times during binding,
  48.                 // it is recommended to bind controls to the result of the Execute method.
  49.                 this.departmentList.DisplayMember = "Name";
  50.                 // this.departmentList.DataSource = ((ObjectQuery)departmentQuery).Execute(MergeOption.AppendOnly);
  51.                 this.departmentList.DataSource = departmentQuery.ToList();
  52.             }
  53.             catch (Exception ex)
  54.             {
  55.                 MessageBox.Show(ex.Message);
  56.             }
  57.         }
  58.  
  59.         private void departmentList_SelectedIndexChanged(object sender, EventArgs e)
  60.         {
  61.             try
  62.             {
  63.                 //Get the object for the selected department.
  64.                 Department department = (Department)this.departmentList.SelectedItem;
  65.  
  66.                 //Bind the grid view to the collection of Course objects
  67.                 // that are related to the selected Department object.
  68.                 //-  courseGridView.DataSource = department.Courses;
  69.                 //- courseGridView.DataSource = department.ToList();
  70.                 courseGridView.DataSource = department.Courses.ToList();
  71.  
  72.                 // Hide the columns that are bound to the navigation properties on Course.
  73.                 courseGridView.Columns["Department"].Visible = false;
  74.                 courseGridView.Columns["StudentGrades"].Visible = false;
  75.                 courseGridView.Columns["OnlineCourse"].Visible = false;
  76.                 courseGridView.Columns["OnsiteCourse"].Visible = false;
  77.                 courseGridView.Columns["People"].Visible = false;
  78.                 courseGridView.Columns["DepartmentId"].Visible = false;
  79.  
  80.                 courseGridView.AllowUserToDeleteRows = false;
  81.                 courseGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
  82.             }
  83.             catch (Exception ex)
  84.             {
  85.                 MessageBox.Show(ex.Message);
  86.             }
  87.         }
  88.  
  89.         private void saveChanges_Click(object sender, EventArgs e)
  90.         {
  91.             try
  92.             {
  93.  
  94.                 // Save object changes to the database,
  95.                 // display a message, and refresh the form.
  96.                 schoolContext.SaveChanges();
  97.                 MessageBox.Show("Changes saved to the database.");
  98.                 this.Refresh();
  99.             }
  100.             catch (Exception ex)
  101.             {
  102.                 MessageBox.Show(ex.Message);
  103.             }
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment