コード例 #1
0
        private async void AddButton_Click(object sender, EventArgs e)
        {
            AddEditShop addShop = new AddEditShop();

            addShop.ProductCB.DataSource    = productes;
            addShop.ProductCB.ValueMember   = "Id";
            addShop.ProductCB.DisplayMember = "Name";

            addShop.IncomeCB.DataSource    = incomes;
            addShop.IncomeCB.ValueMember   = "Id";
            addShop.IncomeCB.DisplayMember = "Date";

            DialogResult result = addShop.ShowDialog(this);

            if (result == DialogResult.Cancel)
            {
                return;
            }

            Shop shop = new Shop()
            {
                Product            = (Product)addShop.ProductCB.SelectedItem,
                Income             = (Income)addShop.IncomeCB.SelectedItem,
                Name               = addShop.FirstNameTB.Text,
                IsAvailableProduct = addShop.IsAvailableProductCB.Checked
            };

            context.Shops.Add(shop);
            await context.SaveChangesAsync();

            MessageBox.Show("Shop was added");
            dataGridView1.Refresh();
        }
コード例 #2
0
        private async void EditButton_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;
                }

                Shop shop = await context.Shops.FindAsync(id);

                AddEditShop addShop = new AddEditShop();

                addShop.FirstNameTB.Text             = shop.Name;
                addShop.IsAvailableProductCB.Checked = shop.IsAvailableProduct;
                addShop.ProductCB.SelectedItem       = shop.Product;
                addShop.IncomeCB.SelectedItem        = shop.Income;

                addShop.ProductCB.DataSource    = productes;
                addShop.ProductCB.ValueMember   = "Id";
                addShop.ProductCB.DisplayMember = "Name";

                addShop.IncomeCB.DataSource    = incomes;
                addShop.IncomeCB.ValueMember   = "Id";
                addShop.IncomeCB.DisplayMember = "Date";

                DialogResult result = addShop.ShowDialog(this);

                if (result == DialogResult.Cancel)
                {
                    return;
                }

                shop.Product            = (Product)addShop.ProductCB.SelectedItem;
                shop.Income             = (Income)addShop.IncomeCB.SelectedItem;
                shop.Name               = addShop.FirstNameTB.Text;
                shop.IsAvailableProduct = addShop.IsAvailableProductCB.Checked;

                await context.SaveChangesAsync();

                MessageBox.Show("Shop was updated");
                dataGridView1.Refresh();
            }
        }