using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Learning_Entity_Framework_www.Learnfiles.com_
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        EF_dbEntities database = new EF_dbEntities();

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = database.tbl_test.ToList();

            dataGridView1.Columns["Id"].HeaderText = "کد";
            dataGridView1.Columns["Name"].HeaderText = "نام";
            dataGridView1.Columns["Tell"].HeaderText = "تلفن";
            dataGridView1.Columns["Address"].HeaderText = "آدرس";
            dataGridView1.Columns["Website"].HeaderText = "وب سایت";
        }

        private void button1_Click(object sender, EventArgs e)
        {

            tbl_test table = new tbl_test
            {
                Name=textBox1.Text,
                Tell=textBox2.Text,
                Address=textBox3.Text,
                Website=textBox4.Text
            };

            database.tbl_test.Add(table);
            database.SaveChanges();

            dataGridView1.DataSource = database.tbl_test.ToList();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            int a = int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString());

            tbl_test table = database.tbl_test.First(i => i.Id == a);

            table.Name = textBox1.Text;
            table.Tell = textBox2.Text;
            table.Address = textBox3.Text;
            table.Website = textBox4.Text;
            database.SaveChanges();

            dataGridView1.DataSource = database.tbl_test.ToList();
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int a = int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString());

            tbl_test table = database.tbl_test.First(i => i.Id == a);
            database.tbl_test.Remove(table);
            database.SaveChanges();


            dataGridView1.DataSource = database.tbl_test.ToList();
        }

        private void button4_Click(object sender, EventArgs e)
        {            
                var qsreach = database.tbl_test.Where(i => i.Name.Contains(textBox5.Text));
                dataGridView1.DataSource = qsreach.ToList();                       
        }
    }
}
