Wednesday, 28 September 2016

C# entity framework 1






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

namespace entity_frame
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            var db = new InventoryEntities();
            var suppliers = db.Suppliers.ToList();
            comboBox1.DataSource = suppliers;
            comboBox1.DisplayMember = "Name";
            comboBox1.ValueMember = "ID";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var db = new InventoryEntities();
            var result = db.Products.
                Where(p => p.Supplier.Name == comboBox1.Text).
                Select(p => new { Product = p.Name, p.Quantity, p.Price, CompanyName = p.Supplier.Name }).
                ToList();

            dataGridView1.DataSource = result;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var supp = new Supplier { Name = textBox1.Text };
            var db = new InventoryEntities();
            db.Suppliers.Add(supp);
            db.SaveChanges();
        }
    }
}

reference:

No comments:

Post a Comment