DB dan muayyan jamoa o’chirilganda Player

Teams { get; set; }
}
}
Bizning grafikli loyihamiz to’rtta formadan iborat bo’ladi: futbolchilar ro'yxati, jamoalar ro'yxati, futbolchini qo’shish/o’zgartirish va jamoani qo’shish/o’zgartirish.
Futbolchilarni ifodalovchi forma quyidagi ko’rinishga ega bo’lsin:
Ushbu formadagi DatatGridView elementi ma’lumotlarni ifodalash uchun, uchta tugma (qo’shish/o’chirish/o’zgartirish) va futbol jamoalarini chaqiriluvchi bitta tugmadan iborat.
Yuqoridagi mavzularda ma’lumotlarni DataGridView ga bog‘lashni ko’rib chiqqan edik. Ma’lumotlarni bog‘lash jarayonida har bir xususiyatga mos ustun hosil qilinadi. Ammo bizga TeamId xususiyati zarur emas. Shuningdek, DataGridView dagi ustun sarlavhalarida xususiyatlarning nomlari yozilishi maqsadga muvofiq. Shuning uchun DatatGridView ni tanlab, xususiyatlar oynasidan ni DataSource tanlaymiz. Ushbu xususiyatga qiymat tanlash vaqtida quyidagi interfeys shakllantiriladi:
Ushbu muloqot oynasidan Add Project Data Source...ni tanlaymiz. Shundan so’ng bizga ma’lumotlar manbaasi oynasi shakllantirilib, undan Object ni tanlashimiz lozim.
Keyingi qadamda bizda loyiha tuzilmasi shakllantiriladi va ushbu tuzilmadan Player klassini topib olamiz:
Shundan so’ng DataGridView da klass xususiyatga mos ustunlar shakllantiriladi. DataGridView dagi Columns xususiyatiga o'tamiz. xususitiga kerakli ustunlaga mos qiymatlarni beramiz va ushbu qiymatlar DataGridView dagi mos ustunlarga sarlavha sifatida namoyish qilinadi. TeamId ustunini esa o’chiramiz:
Yuqorida keltirilgan misoldagi kabi Team klassi uchun mos formani shakllantiramiz:
Ushbu formadagi DataGridView ob’ektida jamoalar ro'yxati chiqariladi. Shuningdek formada bitta ListBox va 'Tarkib' tugmasi ham mavjud. Ushbu ro'yxatda tanlangan jamoada mavjud futbolchilar ro'yxati chiqariladi.
Loyihaga futbolchi va jamoa qo’shish/o’zgartirish uchun formani ham qo’shish lozim. Futbolchi uchun formani PlayerForm kabi nomlaymiz:
Ushbu formada futbolchi FIOsi uchun matnli maydon, yosh uchun NumericUpDown va ikkita ComboBox elementi mavjud.
“OK” tugmasidagi DialogResult xususiyatiga va “Отмена” tugmasidagi DialogResult xususiyatiga Cancel qiymatini beramiz. Barcha maydonlarda Modifiers ni Private dan Protected Internal ga o’zgartiramiz.
Yangi jamoa hosil qilish TeamForm formasi quyidagicha:
Tugmalar va maydonlar uchun DialogResult va Modifiers xususiyatlarini avvalgi forma kabi o’zgartiramiz.
Futbolchilar ro'yxatidan tarkib topgan bosh forma kodi quyidagicha:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace OneToMore
{
public partial class Form1 : Form
{
SoccerContext db;
public Form1()
{
InitializeComponent();
db = new SoccerContext();
db.Players.Load();
dataGridView1.DataSource = db.Players.Local.ToBindingList();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
PlayerForm plForm = new PlayerForm();
// db dan jamoa ro’yxatini shakllantirish
List teams = db.Teams.ToList();
plForm.comboBox2.DataSource = teams;
plForm.comboBox2.ValueMember = "Id";
plForm.comboBox2.DisplayMember = "Name";
DialogResult result = plForm.ShowDialog(this);
if (result == DialogResult.Cancel)
return;
Player player = new Player();
player.Age = (int)plForm.numericUpDown1.Value;
player.Name = plForm.textBox1.Text;
player.Position = plForm.comboBox1.SelectedItem.ToString();
player.Team = (Team)plForm.comboBox2.SelectedItem;
db.Players.Add(player);
db.SaveChanges();
MessageBox.Show("Yangi futbolchi qo’shildi!");
}
private void button2_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
int index = dataGridView1.SelectedRows[0].Index;
int id = 0;
bool converted = Int32.TryParse(dataGridView1[0, index].Value.ToString(), out id);
if (converted == false)
return;
Player player = db.Players.Find(id);
PlayerForm plForm = new PlayerForm();
plForm.numericUpDown1.Value = player.Age;
plForm.comboBox1.SelectedItem = player.Position;
plForm.textBox1.Text = player.Name;
List teams = db.Teams.ToList();
plForm.comboBox2.DataSource = teams;
plForm.comboBox2.ValueMember = "Id";
plForm.comboBox2.DisplayMember = "Name";
if (player.Team != null)
plForm.comboBox2.SelectedValue = player.Team.Id;
DialogResult result = plForm.ShowDialog(this);
if (result == DialogResult.Cancel)
return;
player.Age = (int)plForm.numericUpDown1.Value;
player.Name = plForm.textBox1.Text;
player.Position = plForm.comboBox1.SelectedItem.ToString();
player.Team = (Team)plForm.comboBox2.SelectedItem;
db.Entry(player).State = EntityState.Modified;
db.SaveChanges();
MessageBox.Show("Obyekt yangilandi!");
}
}
private void button3_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
int index = dataGridView1.SelectedRows[0].Index;
int id = 0;
bool converted = Int32.TryParse(dataGridView1[0, index].Value.ToString(), out id);
if (converted == false)
return;
Player player = db.Players.Find(id);
db.Players.Remove(player);
db.SaveChanges();
MessageBox.Show("Obyekt o’chirildi");
}
}
private void button4_Click(object sender, EventArgs e)
{
TeamForm teams = new TeamForm();
teams.Show();
}
}
}
Avvalgi misollarda ham xuddi yuqorida keltirilgan kodda o’xshash amallar bajarilgan. Faqat qo’shimcha tarzda jamoani tanlash maydoni qo’shib qo’yilgan:
List teams = db.Teams.ToList();
plForm.comboBox2.DataSource = teams;
plForm.comboBox2.ValueMember = "Id";
plForm.comboBox2.DisplayMember = "Name";
Tahrirlash jarayonida esa ushbu maydon qiymatini quyidagi kod asosida amalga oshiramiz
plForm.comboBox2.SelectedValue = player.Team.Id;