private void button2_Click(object sender, EventArgs e)
        {
            using (The_State_Duma_Entities db = new The_State_Duma_Entities(DBUtils.getConnString()))
            {
                FrakciyaForm addForm = new FrakciyaForm();

                List <Deputaty> deps = db.Deputaties.ToList();
                for (int i = deps.Count - 1; i >= 0; i--)
                {
                    var dep = deps[i];

                    if (dep.Frakciya != null)
                    {
                        deps.RemoveAt(i);
                    }
                }

                DialogResult result = addForm.ShowDialog(this);

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

                Frakciya frak = new Frakciya();

                frak.Naimenovanie = addForm.textBox1.Text;

                using (var transaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        db.Frakciyas.Add(frak);

                        try
                        {
                            db.SaveChanges();
                        }
                        catch (DbUpdateException ex)
                        {
                            MessageBox.Show("Ошибка добавления : " + ex.ToString());
                        }

                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Ошибка добавления : " + ex.ToString());

                        try { transaction.Rollback(); }
                        catch (Exception ex2)
                        {
                            MessageBox.Show("Ошибка отката : " + ex2.ToString());
                        }
                    }
                }
                RefreshData();
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            using (The_State_Duma_Entities db = new The_State_Duma_Entities(DBUtils.getConnString()))
            {
                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;
                    }

                    Frakciya frak = db.Frakciyas.Find(id);
                    listBox1.DataSource    = frak.Deputaties.ToList();
                    listBox1.DisplayMember = "Familiya";
                }
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            using (The_State_Duma_Entities db = new The_State_Duma_Entities(DBUtils.getConnString()))
            {
                if (dataGridView1.SelectedRows.Count < 1)
                {
                    return;
                }

                int  index     = dataGridView1.SelectedRows[0].Index;
                int  id        = 0;
                bool converted = Int32.TryParse(dataGridView1[0, index].Value.ToString(), out id);
                if (converted == false)
                {
                    return;
                }

                Frakciya frak = db.Frakciyas.Find(id);

                FrakciyaForm addForm = new FrakciyaForm();

                addForm.textBox1.Text = frak.Naimenovanie;

                List <Deputaty> deps = db.Deputaties.ToList();
                for (int i = deps.Count - 1; i >= 0; i--)
                {
                    if (deps[i].ID_frakcii != id)
                    {
                        deps.RemoveAt(i);
                    }
                }

                addForm.comboBox1.DataSource    = deps;
                addForm.comboBox1.ValueMember   = "Partiyniy_bilet";
                addForm.comboBox1.DisplayMember = "Familiya";

                addForm.comboBox1.SelectedItem = frak.Lider_frakcii;

                DialogResult result = addForm.ShowDialog(this);

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

                frak.Naimenovanie = addForm.textBox1.Text;
                if (addForm.comboBox1.SelectedItem != null)
                {
                    frak.Deputaty = (Deputaty)addForm.comboBox1.SelectedItem;
                }
                else
                {
                    frak.Deputaty = null;
                }
                if (addForm.comboBox1.SelectedValue != null)
                {
                    frak.Lider_frakcii = (int)addForm.comboBox1.SelectedValue;
                }
                else
                {
                    frak.Lider_frakcii = null;
                }

                using (var transaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        db.Entry(frak).State = EntityState.Modified;

                        try
                        {
                            db.SaveChanges();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Ошибка обновления : " + ex.ToString());
                        }

                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Ошибка обновления : " + ex.ToString());
                        try { transaction.Rollback(); }
                        catch (Exception ex2)
                        {
                            MessageBox.Show("Ошибка отката : " + ex2.ToString());
                        }
                    }
                }
                RefreshData();
            }
        }