コード例 #1
0
        private void buttonDeleteUser_Click(object sender, EventArgs e)
        {
            int selectedRowCount = dataGridView2.Rows.GetRowCount(DataGridViewElementStates.Selected);

            try
            {
                using (testEntities context = new testEntities())
                {
                    for (int i = 0; i < selectedRowCount; i++)
                    {
                        DataGridViewRow row       = dataGridView2.SelectedRows[i];
                        int             rowId     = Int32.Parse(row.Cells[0].Value.ToString());
                        TableTest       forDelete = context.TableTests.FirstOrDefault(u => u.id == rowId);

                        context.TableTests.Remove(forDelete);
                    }

                    context.SaveChanges();
                    RefreshGrid();
                }
            }
            catch (ArgumentOutOfRangeException ex)
            {
                MessageBox.Show("Вы не выбрали сотрудника для удаления");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
コード例 #2
0
        private void buttonAddUser_Click(object sender, EventArgs e)
        {
            try
            {
                string name        = textBoxName.Text;
                string surname     = textBoxSurname.Text;
                string thirdName   = textBoxThirdName.Text;
                string jobPosition = textBoxJobPosition.Text;
                int    salary      = Decimal.ToInt32(numericSalary.Value);

                DateTime            hireDate = dateTimePickerFired.Value.Date;
                Nullable <DateTime> fireDate;

                if (checkBoxIsFired.Checked)
                {
                    fireDate = dateTimePickerFired.Value.Date;
                }
                else
                {
                    fireDate = null;
                }

                TableTest newUser = new TableTest
                {
                    Name        = name,
                    Surname     = surname,
                    ThirdName   = thirdName,
                    JobPosition = jobPosition,
                    Salary      = salary,
                    HireDate    = hireDate,
                    FireDate    = fireDate
                };

                using (testEntities context = new testEntities())
                {
                    context.TableTests.Add(newUser);
                    context.SaveChanges();
                }
            }
            catch when(ContainInappropriateSymbols(textBoxName))
            {
                MessageBox.Show("Введенноё имя не соответствует требованиям");
            }