コード例 #1
0
        private void btnAdd_Click(object sender, EventArgs e)
        {
            DFormDriver  dFormDriver  = new DFormDriver();
            DialogResult dialogResult = dFormDriver.ShowDialog();

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

            Driver driver = new Driver
            {
                Name            = dFormDriver.tbName.Text,
                Phone           = dFormDriver.tbPhone.Text,
                DrivingCategory = dFormDriver.tbDrivengCategory.Text,
                Comments        = dFormDriver.rtbComments.Text
            };

            db.Drivers.Add(driver);
            db.SaveChanges();

            SearchObjects(this, EventArgs.Empty);

            MessageBox.Show("Новый объект добавлен.", "Информация", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
コード例 #2
0
        private void btnChange_Click(object sender, EventArgs e)
        {
            if (dgvDrivers.SelectedRows.Count > 0)
            {
                int  selectedIndex = dgvDrivers.SelectedRows[0].Index;
                int  id            = 0;
                bool converted     = int.TryParse(dgvDrivers[0, selectedIndex].Value.ToString(), out id);

                if (!converted)
                {
                    return;
                }

                Driver driver = db.Drivers.Find(id);

                DFormDriver dFormDriver = new DFormDriver();
                dFormDriver.tbName.Text            = driver.Name;
                dFormDriver.tbPhone.Text           = driver.Phone;
                dFormDriver.tbDrivengCategory.Text = driver.DrivingCategory;
                dFormDriver.rtbComments.Text       = driver.Comments;

                DialogResult dialogResult = dFormDriver.ShowDialog(this);

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

                driver.Name            = dFormDriver.tbName.Text;
                driver.Phone           = dFormDriver.tbPhone.Text;
                driver.DrivingCategory = dFormDriver.tbDrivengCategory.Text;
                driver.Comments        = dFormDriver.rtbComments.Text;

                db.SaveChanges();
                dgvDrivers.Refresh();

                LoadComments(this, EventArgs.Empty);

                MessageBox.Show("Объект обновлен.", "Информация", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }