Beispiel #1
0
        private void EditSupplierButton_Click(object sender, EventArgs e)
        {
            //get the key of the current Supplier in the data grid view
            int    rowNum     = supplierDataGridView.CurrentCell.RowIndex;
            string supplierID = supplierDataGridView["dataGridViewTextBoxColumn1", rowNum].Value.ToString();
            //create a current Supplier object
            Supplier existingSupplier;

            using (SupplierDataContext dataContext = new SupplierDataContext())
            {
                existingSupplier = (from s in dataContext.Suppliers
                                    where s.SupplierId.ToString() == supplierID
                                    select s).Single();
            }
            //instantiate the addeditSupplierForm
            frmAddEditSupplier frmAddEdit = new frmAddEditSupplier();

            frmAddEdit.EditMode          = true;                           //editing Supplier
            frmAddEdit.exsistingSupplier = existingSupplier;
            DialogResult result = frmAddEdit.ShowDialog();                 // display second form modal

            if (result == DialogResult.OK || result == DialogResult.Retry) // successful update or concurrency exception
            {
                reloadGridView();
            }
        }
        private void Updatebutton_Click(object sender, EventArgs e)
        {
            if (!EditMode)                          //if adding a Supplier
            {
                Supplier newSupplier = new Supplier //create a new Supplier from user input
                {
                    SupplierId = int.Parse(supplierIdTextBox.Text),
                    SupName    = supNameTextBox.Text
                };
                using (SupplierDataContext dataContext = new SupplierDataContext())
                {
                    //insert through data context from the main form
                    dataContext.Suppliers.InsertOnSubmit(newSupplier);
                    dataContext.SubmitChanges(); //submit to the database
                }
                DialogResult = DialogResult.OK;
            }
            else //if editing Supplier
            {
                try
                {
                    using (SupplierDataContext dataContext = new SupplierDataContext())
                    {
                        //get the product ID from current text box
                        Supplier supplier = dataContext.Suppliers.Single(p => p.SupplierId.ToString() == supplierIdTextBox.Text);

                        if (supplier != null)
                        {
                            supplier.SupName = supNameTextBox.Text;
                            //submit changes
                            dataContext.SubmitChanges();
                            DialogResult = DialogResult.OK;
                        }
                    }
                }
                catch (ChangeConflictException)
                {
                    MessageBox.Show("Another user changed or deleted the current record", "Concurrency Exception");
                    DialogResult = DialogResult.Retry;
                }
                catch (Exception excp)
                {
                    MessageBox.Show(excp.Message, excp.GetType().ToString());
                }
            }
        }
Beispiel #3
0
        private void FrmSupplier_Load(object sender, EventArgs e)
        {
            using (SupplierDataContext dataContext = new SupplierDataContext())
            {
                supplierDataGridView.DataSource = dataContext.Suppliers;
            }

            supplierDataGridView.BorderStyle = BorderStyle.None;
            supplierDataGridView.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(238, 239, 249);
            supplierDataGridView.CellBorderStyle = DataGridViewCellBorderStyle.SingleHorizontal;
            supplierDataGridView.DefaultCellStyle.SelectionBackColor = Color.DarkTurquoise;
            supplierDataGridView.DefaultCellStyle.SelectionForeColor = Color.WhiteSmoke;
            supplierDataGridView.BackgroundColor = Color.White;

            supplierDataGridView.EnableHeadersVisualStyles = false;
            supplierDataGridView.ColumnHeadersBorderStyle  = DataGridViewHeaderBorderStyle.None;
            supplierDataGridView.ColumnHeadersDefaultCellStyle.BackColor = Color.FromArgb(20, 25, 72);
            supplierDataGridView.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
        }
Beispiel #4
0
        private void reloadGridView()
        {
            SupplierDataContext dataContext = new SupplierDataContext();

            supplierDataGridView.DataSource = dataContext.Suppliers;
        }