Programlama ile ilgili örnekler:
https://github.com/tbagriyanik
https://github.com/tbagriyanik/WinForms_List_Class_puanTablosu-6-
https://github.com/tbagriyanik/WinForms_EntityFramework6-11-
https://github.com/tbagriyanik/WinForms_EF_login-13-
https://github.com/tbagriyanik/WinForms_CRUD-15-
https://github.com/tbagriyanik/WinForms-CodeFirst-16-
https://github.com/tbagriyanik/WinForms-CodeFirst-sozlukApp-19-
https://github.com/tbagriyanik/WinForms-CodeFirst-OgrenciNot-21-
İngilizce kaynak:
https://www.c-sharpcorner.com/article/crud-operation-windows-form-app-using-entity-framework
Form üzerine gerekli nesneleri ekleyiniz.
Projeye önce “Hizmet tabanlı veritabanı” yani ikinciVT.mdf dosyası, sonra da “ADO.NET Entity Data Model” yani Model1.edmx ekleyiniz.
Tablo Yapısı:
CREATE TABLE [dbo].[yemek] (
[kimlik] INT IDENTITY (1, 1) NOT NULL,
[adi] NVARCHAR (50) NOT NULL,
[tarif] NTEXT NULL,
[tatliMi] BIT NULL,
[fiyat] MONEY NULL,
[resim] IMAGE NULL,
[uretimTarihi] DATETIME NULL,
PRIMARY KEY CLUSTERED ([kimlik] ASC)
);
Program test edilebilir. Setup veya Install yapımı için “Derle*Yayımla h11vt” ile gelen pencereden kolayca yapılabilir. Proje kodları kurulum içine dahil edilmeyecektir.
Proje kodları:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace h11vt
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//ADO.NET kütüphanesi sayesinde bağlantı yapalım
ikinciVTEntities vt = new ikinciVTEntities();
int seciliId = 0;
private void Form1_Load(object sender, EventArgs e)
{
//vt bağlantısı yap
dataGridView1.DataSource = vt.yemeks.ToList();
açToolStripButton.PerformClick(); //ilk açılışta ilk kayıt bilgileri gelsin
}
private void yeniToolStripButton_Click(object sender, EventArgs e)
{
//yeni kayıt ekle
yemek yeni = new yemek();
yeni.adi = textBox1.Text.Trim();
yeni.tarif = richTextBox1.Text.Trim();
yeni.tatliMi = checkBox1.Checked;
yeni.uretimTarihi = dateTimePicker1.Value;
if (textBox2.Text.Trim() != "")
yeni.fiyat = Convert.ToDecimal(textBox2.Text.Trim());
else
yeni.fiyat = 0;
if (pictureBox1.ImageLocation != null)
yeni.resim = ConvertFiletoByte(pictureBox1.ImageLocation);
label7.Text = "...";
vt.yemeks.Add(yeni);
vt.SaveChanges();
dataGridView1.DataSource = vt.yemeks.ToList();
}
private void açToolStripButton_Click(object sender, EventArgs e)
{
//kayıt getir
if (dataGridView1.Rows.Count == 0) return;//hiç kayıt yoksa?
seciliId = (int)dataGridView1.CurrentRow.Cells[0].Value;
label7.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString(); //kimlik
textBox1.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString(); //null olamayan adi
if (dataGridView1.CurrentRow.Cells[2].Value != null)
richTextBox1.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
else
richTextBox1.Text = "";
if (dataGridView1.CurrentRow.Cells[3].Value != null)
checkBox1.Checked = (dataGridView1.CurrentRow.Cells[3].Value.ToString() == "True");
else
checkBox1.Checked = false;
if (dataGridView1.CurrentRow.Cells[6].Value != null)
dateTimePicker1.Value = DateTime.Parse(dataGridView1.CurrentRow.Cells[6].Value.ToString());
else
dateTimePicker1.Value = DateTime.Now;
if (dataGridView1.CurrentRow.Cells[4].Value != null)
textBox2.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
else
textBox2.Text = "0";
seciliId = (int)dataGridView1.CurrentRow.Cells[0].Value;
yemek sadeceGetir = vt.yemeks.Where(x => x.kimlik == seciliId).FirstOrDefault();
if (sadeceGetir.resim != null)
pictureBox1.Image = ConvertBytetoImage(sadeceGetir.resim);
else
pictureBox1.Image = null;
}
private void kaydetToolStripButton_Click(object sender, EventArgs e)
{
//güncelle / kaydet
seciliId = (int)dataGridView1.CurrentRow.Cells[0].Value;
yemek guncellenecek = vt.yemeks.Where(x => x.kimlik == seciliId).FirstOrDefault();
guncellenecek.adi = textBox1.Text.Trim();
if (textBox2.Text != "")
guncellenecek.fiyat = Convert.ToDecimal(textBox2.Text.Trim());
else
guncellenecek.fiyat = 0;
guncellenecek.tarif = richTextBox1.Text.Trim();
guncellenecek.tatliMi = checkBox1.Checked;
guncellenecek.uretimTarihi = dateTimePicker1.Value;
label7.Text = seciliId.ToString();
if (pictureBox1.ImageLocation != null)
guncellenecek.resim = ConvertFiletoByte(pictureBox1.ImageLocation);
vt.SaveChanges();
dataGridView1.DataSource = vt.yemeks.ToList();
}
private void kesToolStripButton_Click(object sender, EventArgs e)
{
//kayıt sil
if (MessageBox.Show("Yemeği silmek için emin misiniz?", "Silme",
MessageBoxButtons.YesNo,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button2) == DialogResult.Yes)
{
seciliId = (int)dataGridView1.CurrentRow.Cells[0].Value;
yemek silinecek = vt.yemeks.Where(x => x.kimlik == seciliId).FirstOrDefault();
label7.Text = seciliId.ToString();
vt.yemeks.Remove(silinecek);
vt.SaveChanges();
dataGridView1.DataSource = vt.yemeks.ToList();
}
}
private Image ConvertBytetoImage(byte[] photo)
{
Image newImage;
using (MemoryStream ms = new MemoryStream(photo, 0, photo.Length))
{
ms.Write(photo, 0, photo.Length);
newImage = Image.FromStream(ms, true);
}
return newImage;
}
private byte[] ConvertFiletoByte(string sPath)
{
byte[] data = null;
FileInfo fInfo = new FileInfo(sPath);
long numBytes = fInfo.Length;
FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fStream);
data = br.ReadBytes((int)numBytes);
return data;
}
private void button1_Click(object sender, EventArgs e)
{
//resim seçme JPG PNG GIF
//alıntı kaynağı: https://www.youtube.com/watch?v=oINYRlo_3oA
OpenFileDialog resimSec = new OpenFileDialog();
resimSec.Title = "Resim Seç";
resimSec.Filter = "JPG|*.jpg|PNG|*.png|GIF|*.gif";
resimSec.Multiselect = false;
if (resimSec.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
pictureBox1.ImageLocation = resimSec.FileName;
}
}
}
}