Professional Documents
Culture Documents
Dẫn nhập
Nội dung
Kết luận
Thảo luận
Dẫn nhập
Sức mạnh của hệ điều hành Window là không thể chối cãi. Và để tạo nên sức mạnh đó không thể thiếu những ứng dụng mạnh mẽ. Vậy để tạo ra những
ứng dụng đó, người lập trình viên cần học cái gì?
Nội dung
SinhVien.cs
1 //------------------------------------------------------------------------------
2 // <auto-generated>
3 // This code was generated from a template.
4 //
5 // Manual changes to this file may cause unexpected behavior in your application.
6 // Manual changes to this file will be overwritten if the code is regenerated.
7 // </auto-generated>
8 //------------------------------------------------------------------------------
9
10 namespace EntityGUI
11 {
12 using System;
13 using System.Collections.Generic;
14
15 public partial class SinhVien
16 {
17 public int ID { get; set; }
18 public string Name { get; set; }
19 public int IDLop { get; set; }
20
21 public virtual Lop Lop { get; set; }
22 }
23 }
24
Lop.cs
https://howkteam.vn/course/lap-trinh-winform-co-ban/entity-framework-trong-lap-trinh-c-winform-1313 2/10
11:44 23/02/2023 Entity Framework trong lập trình C# Winform | How Kteam
1 //------------------------------------------------------------------------------
2 // <auto-generated>
3 // This code was generated from a template.
4 //
5 // Manual changes to this file may cause unexpected behavior in your application.
6 // Manual changes to this file will be overwritten if the code is regenerated.
7 // </auto-generated>
8 //------------------------------------------------------------------------------
9
10 namespace EntityGUI
11 {
12 using System;
13 using System.Collections.Generic;
14
15 public partial class Lop
16 {
17 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
18 public Lop()
19 {
20 this.SinhVien = new HashSet<SinhVien>();
21 }
22
23 public int ID { get; set; }
24 public string Name { get; set; }
25
26 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
27 public virtual ICollection<SinhVien> SinhVien { get; set; }
28 }
29 }
30
Form1.cs
https://howkteam.vn/course/lap-trinh-winform-co-ban/entity-framework-trong-lap-trinh-c-winform-1313 3/10
11:44 23/02/2023 Entity Framework trong lập trình C# Winform | How Kteam
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Threading.Tasks;
9 using System.Windows.Forms;
10
11 namespace EntityGUI
12 {
13 public partial class Form1 : Form
14 {
15 KteamEntities db = new KteamEntities();
16 public Form1()
17 {
18 InitializeComponent();
19 LoadData();
20 AddBinding();
21 }
22
23 #region methods
24 void AddBinding()
25 {
26 txbID.DataBindings.Add(new Binding("Text", dtgvData.DataSource, "IDLop", true, DataSourceUpdateMode.Never));
27 txbName.DataBindings.Add(new Binding("Text", dtgvData.DataSource, "Name", true, DataSourceUpdateMode.Never));
28 }
29 void LoadData()
30 {
31 //using (KteamEntities db = new KteamEntities())
32 {
33 var result = from c in db.SinhVien
34 //where c.ID > 1 && c.ID < 4
35 select c;
36
37 //var result = db.SinhVien.Find(2);
38
39 dtgvData.DataSource = result.ToList();
40 }
41 }
42 void AddSinhVien()
43 {
44 //using (KteamEntities db = new KteamEntities())
45 {
46 SinhVien sv = new SinhVien() { Name = txbName.Text, IDLop = Convert.ToInt32(txbID.Text)};
47 db.SinhVien.Add(sv);
48 db.SaveChanges();
49 }
50 }
51 void DeleteSinhVien()
52 {
53 int id = Convert.ToInt32(txbID.Text);
54 SinhVien sv = db.SinhVien.Where(p => p.IDLop == id && p.Name == txbName.Text).SingleOrDefault();
55 db.SinhVien.Remove(sv);
56 db.SaveChanges();
57 }
58 void EditSinhVien()
59 {
60 int id = Convert.ToInt32(dtgvData.SelectedCells[0].OwningRow.Cells["ID"].Value.ToString());
61 SinhVien sv = db.SinhVien.Find(id);
62 sv.Name = txbName.Text;
63 sv.IDLop = Convert.ToInt32(txbID.Text);
64 db.SaveChanges();
65 }
66 #endregion
67
68 #region Events
69 private void btnShow_Click(object sender, EventArgs e)
70 {
71 LoadData();
72 }
73
74 private void btnAdd_Click(object sender, EventArgs e)
75 {
76 AddSinhVien();
77 }
78
79 private void btnDelete_Click(object sender, EventArgs e)
80 {
https://howkteam.vn/course/lap-trinh-winform-co-ban/entity-framework-trong-lap-trinh-c-winform-1313 4/10
11:44 23/02/2023 Entity Framework trong lập trình C# Winform | How Kteam
81 DeleteSinhVien();
82 }
83
84 private void btnEdit_Click(object sender, EventArgs e)
85 {
86 EditSinhVien();
87 }
88 #endregion
89 }
90 }
91
Form1.Designer.cs
https://howkteam.vn/course/lap-trinh-winform-co-ban/entity-framework-trong-lap-trinh-c-winform-1313 5/10
11:44 23/02/2023 Entity Framework trong lập trình C# Winform | How Kteam
1 namespace EntityGUI
2 {
3 partial class Form1
4 {
5 /// <summary>
6 /// Required designer variable.
7 /// </summary>
8 private System.ComponentModel.IContainer components = null;
9
10 /// <summary>
11 /// Clean up any resources being used.
12 /// </summary>
13 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
14 protected override void Dispose(bool disposing)
15 {
16 if (disposing && (components != null))
17 {
18 components.Dispose();
19 }
20 base.Dispose(disposing);
21 }
22
23 #region Windows Form Designer generated code
24
25 /// <summary>
26 /// Required method for Designer support - do not modify
27 /// the contents of this method with the code editor.
28 /// </summary>
29 private void InitializeComponent()
30 {
31 this.dtgvData = new System.Windows.Forms.DataGridView();
32 this.txbID = new System.Windows.Forms.TextBox();
33 this.txbName = new System.Windows.Forms.TextBox();
34 this.btnShow = new System.Windows.Forms.Button();
35 this.btnAdd = new System.Windows.Forms.Button();
36 this.btnDelete = new System.Windows.Forms.Button();
37 this.btnEdit = new System.Windows.Forms.Button();
38 ((System.ComponentModel.ISupportInitialize)(this.dtgvData)).BeginInit();
39 this.SuspendLayout();
40 //
41 // dtgvData
42 //
43 this.dtgvData.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
44 this.dtgvData.Location = new System.Drawing.Point(12, 40);
45 this.dtgvData.Name = "dtgvData";
46 this.dtgvData.Size = new System.Drawing.Size(239, 323);
47 this.dtgvData.TabIndex = 0;
48 //
49 // txbID
50 //
51 this.txbID.Location = new System.Drawing.Point(257, 40);
52 this.txbID.Name = "txbID";
53 this.txbID.Size = new System.Drawing.Size(100, 20);
54 this.txbID.TabIndex = 1;
55 //
56 // txbName
57 //
58 this.txbName.Location = new System.Drawing.Point(257, 77);
59 this.txbName.Name = "txbName";
60 this.txbName.Size = new System.Drawing.Size(100, 20);
61 this.txbName.TabIndex = 2;
62 //
63 // btnShow
64 //
65 this.btnShow.Location = new System.Drawing.Point(257, 103);
66 this.btnShow.Name = "btnShow";
67 this.btnShow.Size = new System.Drawing.Size(75, 23);
68 this.btnShow.TabIndex = 3;
69 this.btnShow.Text = "Xem";
70 this.btnShow.UseVisualStyleBackColor = true;
71 this.btnShow.Click += new System.EventHandler(this.btnShow_Click);
72 //
73 // btnAdd
74 //
75 this.btnAdd.Location = new System.Drawing.Point(257, 132);
76 this.btnAdd.Name = "btnAdd";
77 this.btnAdd.Size = new System.Drawing.Size(75, 23);
78 this.btnAdd.TabIndex = 4;
79 this.btnAdd.Text = "Thêm";
80 this.btnAdd.UseVisualStyleBackColor = true;
https://howkteam.vn/course/lap-trinh-winform-co-ban/entity-framework-trong-lap-trinh-c-winform-1313 6/10
11:44 23/02/2023 Entity Framework trong lập trình C# Winform | How Kteam
Download project
Kết luận
Tham khảo thêm nhiều khóa học Lập trình C#.net thực tế khác tại Howkteam.com
Cảm ơn các bạn đã theo dõi bài viết. Hãy để lại bình luận hoặc góp ý của mình để phát triển bài viết tốt hơn. Đừng quên “Luyện tập – Thử thách –
Không ngại khó”.
Thảo luận
Nếu bạn có bất kỳ khó khăn hay thắc mắc gì về khóa học, đừng ngần ngại đặt câu hỏi trong phần BÌNH LUẬN bên dưới hoặc trong mục HỎI & ĐÁP trên
thư viện Howkteam.com để nhận được sự hỗ trợ từ cộng đồng.
https://howkteam.vn/course/lap-trinh-winform-co-ban/entity-framework-trong-lap-trinh-c-winform-1313 7/10