Example #1
0
        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            var          loc  = e.Location;
            DataGridView oDgv = sender as DataGridView;

            if (oDgv != null && e.Button.ToString() == "Right")
            {
                if (oDgv.SelectedRows.Count > 0)
                {
                    DialogResult res = MessageBox.Show("Please confirm this transaction", "Confirmation Required", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    using (var context = new TTI2Entities())
                    {
                        if (res == DialogResult.OK)
                        {
                            var          Pk     = (int)oDgv.CurrentRow.Cells[0].Value;
                            TLADM_Shifts shifts = context.TLADM_Shifts.Find(Pk);
                            if (shifts != null)
                            {
                                context.TLADM_Shifts.Remove(shifts);

                                try
                                {
                                    context.SaveChanges();
                                    oDgv.Rows.Clear();
                                    MessageBox.Show("Data successfully saved to database");
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show(ex.Message);
                                    return;
                                }
                            }
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Please select a row in the datagrid", "Information");
                }
            }
        }
Example #2
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            Button oBtn    = sender as Button;
            bool   success = true;
            bool   lAdd;

            if (oBtn != null && formloaded)
            {
                var Department = (TLADM_Departments)cmbDepartments.SelectedItem;
                if (Department != null)
                {
                    using (var context = new TTI2Entities())
                    {
                        foreach (DataGridViewRow dr in dataGridView1.Rows)
                        {
                            TLADM_Shifts shifts = new TLADM_Shifts();

                            if (dr.Cells[1].Value == null)
                            {
                                continue;
                            }

                            lAdd = true;

                            if (dr.Cells[0].Value != null)
                            {
                                if ((int)dr.Cells[0].Value != 0)
                                {
                                    lAdd   = false;
                                    shifts = context.TLADM_Shifts.Find((int)dr.Cells[0].Value);
                                }
                            }

                            shifts.Shft_Dept_FK     = Department.Dep_Id;
                            shifts.Shft_Description = dr.Cells[1].Value.ToString();
                            shifts.Shft_Start       = (TimeSpan)dr.Cells[2].Value;
                            shifts.Shft_End         = (TimeSpan)dr.Cells[3].Value;

                            if (lAdd)
                            {
                                context.TLADM_Shifts.Add(shifts);
                            }

                            try
                            {
                                context.SaveChanges();
                            }
                            catch (Exception ex)
                            {
                                var exceptionMessages = new StringBuilder();
                                do
                                {
                                    exceptionMessages.Append(ex.Message);
                                    ex = ex.InnerException;
                                }while (ex != null);

                                MessageBox.Show(exceptionMessages.ToString());
                                success = false;
                            }
                        }
                    }
                    if (success)
                    {
                        dataGridView1.Rows.Clear();
                        MessageBox.Show("data save to database");
                    }
                }
            }
        }