DataGridView elementning AllowUserToAddRows xususiyatiga False qiymatini, SelectionMode xususiyatiga FullRowSelect qiymatini beramiz.
Ushbu formamiz asosiy forma bo’lganligi sababli, DBga yangi yozuv qo’shish, o’zgartirish va o’chirish amallari uchun alohida formalarni shakllantirish lozim. Loyihaga yangi PlayerForm formasini qo’shamiz:
Ushbu formada futbolchi FIOni kiritish uchun matnli maydon, so’ngra futbolchining maydondagi pozitsiyasini tanlash uchun ComboBox elementini qo’shib qo’yamiz. Ushbu elementda to’rtta pozitsiya mavjud. So’ngra, futbolchi yoshini tanlash uchun NumericUpDown ni shakllantiramiz.
Shuningdek, loyihaga ikkita tugmani qo’shib qo’yamiz. “OK” tugmasining DialogResult xususiyati uchun OK qiymatini, “Отмена” tugmasi DialogResult xususiyati uchun Cancel qiymatini beramiz.
Ushbu forma hech qanday kodni o’zida saqlamaydi. Endi Form1 formamizga qaytamiz. Ushbu formada barcha mantiq joylashadi:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Data.Entity;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace NewModelFirstApp
{
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)
{
}
// yangi yozuv qo’shish
private void button1_Click(object sender, EventArgs e)
{
PlayerForm plForm = new PlayerForm();
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();
db.Players.Add(player);
db.SaveChanges();
MessageBox.Show("Yangi yozuv qo’shildi!");
}
// o’chirish
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("Yozuv o’chirildi");
}
}
// o’zgartirish
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;
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();
db.Entry(player).State = EntityState.Modified;
db.SaveChanges();
MessageBox.Show(“Yozuv o’zgartirildi”);
}
}
}
}
Ushbu kodda ma’lumotlarni DBdan olish uchun db.Players ifodasidan foydalanilgan. Ammo DataGridView elementiga ushbu ob’ektni bog‘lash va jadval ustida amalga oshirilgan o’zgartirishlarni namoyish qilish lozim. Shuning uchun avvalo db.Players.Load() metodi orqali ma’lumotlarni DbContext ga yuklaydi. So’ngra dataGridView1.DataSource = db.Players.Local.ToBindingList() jamoa orqali db.Players va dataGridView1 ob’ektlari bog‘lanadi.
|