Exemplo n.º 1
0
 private void buttonSave_Click(object sender, EventArgs e)
 {
     using(var db = new Animals())
     {
         for (int i = 0; i < amount; i++)
         {
             var animal = new Animal { Name = this.dataGridViewAnimals.Rows[i].Cells[0].Value.ToString(), Breed = this.dataGridViewAnimals.Rows[i].Cells[1].Value.ToString(), Sex = this.dataGridViewAnimals.Rows[i].Cells[2].Value.ToString()};
             db.AnimalSet.Add(animal);
             db.SaveChanges();
         }
     }
     dataGridViewAnimals.Rows.Clear();
     dataGridViewAnimals.Columns.Clear();
     this.dataGridViewAnimals.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
     this.dataGridViewAnimals.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
     this.dataGridViewAnimals.Columns.Add("Name", "Ім'я");
     this.dataGridViewAnimals.Columns.Add("Breed", "Порода");
     this.dataGridViewAnimals.Columns.Add("Sex", "Стать");
     this.dataGridViewAnimals.Columns[0].Width = 90;
     this.dataGridViewAnimals.Columns[1].Width = 90;
     this.dataGridViewAnimals.Columns[2].Width = 90;
     this.dataGridViewAnimals.ReadOnly = true;
     amount = 0;
 }
Exemplo n.º 2
0
        private void buttonFind_Click(object sender, EventArgs e)
        {
            this.dataGridViewFindAnimals.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            this.dataGridViewFindAnimals.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            this.dataGridViewFindAnimals.Columns.Add("Name", "Ім'я");
            this.dataGridViewFindAnimals.Columns.Add("Breed", "Порода");
            this.dataGridViewFindAnimals.Columns.Add("Sex", "Стать");
            this.dataGridViewFindAnimals.Columns[0].Width = 90;
            this.dataGridViewFindAnimals.Columns[1].Width = 90;
            this.dataGridViewFindAnimals.Columns[2].Width = 90;
            this.dataGridViewFindAnimals.ReadOnly = true;

            if (radioButton1.Checked == true)
            {
                using (var db = new Animals())
                {
                    var query = from animal in db.AnimalSet
                                where animal.Name == textBoxFindName.Text
                                select animal;
                    if (query.Count<Animal>() == 0)
                    {
                        MessageBox.Show("Тварин з таким іменем не знайдено.");
                    }
                    else
                    {
                        dataGridViewFindAnimals.Visible = true;
                        int amountFinded = 0;
                        foreach (var item in query)
                        {
                            this.dataGridViewFindAnimals.Rows.Add();
                            this.dataGridViewFindAnimals.Rows[amountFinded].Cells[0].Value = item.Name;
                            this.dataGridViewFindAnimals.Rows[amountFinded].Cells[1].Value = item.Breed;
                            this.dataGridViewFindAnimals.Rows[amountFinded].Cells[2].Value = item.Sex;
                            amountFinded++;
                        }
                    }
                }
            }
            else
            {
                if (radioButton2.Checked == true)
                {
                    using (var db = new Animals())
                    {
                        var query = from animal in db.AnimalSet
                                    where animal.Breed == textBoxFindBreed.Text
                                    select animal;
                        if (query.Count<Animal>() == 0)
                        {
                            MessageBox.Show("Тварин з такою породою не знайдено.");
                        }
                        else
                        {
                            dataGridViewFindAnimals.Visible = true;
                            int amountFinded = 0;
                            foreach (var item in query)
                            {
                                this.dataGridViewFindAnimals.Rows.Add();
                                this.dataGridViewFindAnimals.Rows[amountFinded].Cells[0].Value = item.Name;
                                this.dataGridViewFindAnimals.Rows[amountFinded].Cells[1].Value = item.Breed;
                                this.dataGridViewFindAnimals.Rows[amountFinded].Cells[2].Value = item.Sex;
                                amountFinded++;
                            }
                        }
                    }
                }
                else
                {
                    if (radioButtonFindMale.Checked || radioButtonFindFemale.Checked)
                    {
                        using (var db = new Animals())
                        {
                            string sex = string.Empty;
                            if (this.radioButtonFindMale.Checked == true)
                            {
                                sex = "Чоловік";
                            }
                            else
                            {
                                sex = "Жінка";
                            }
                            var query = from animal in db.AnimalSet
                                        where animal.Sex == sex
                                        select animal;
                            if (query.Count<Animal>() == 0)
                            {
                                MessageBox.Show("Тварин з такою статтю не знайдено.");
                            }
                            else
                            {
                                dataGridViewFindAnimals.Visible = true;
                                int amountFinded = 0;
                                foreach (var item in query)
                                {
                                    this.dataGridViewFindAnimals.Rows.Add();
                                    this.dataGridViewFindAnimals.Rows[amountFinded].Cells[0].Value = item.Name;
                                    this.dataGridViewFindAnimals.Rows[amountFinded].Cells[1].Value = item.Breed;
                                    this.dataGridViewFindAnimals.Rows[amountFinded].Cells[2].Value = item.Sex;
                                    amountFinded++;
                                }
                            }
                        }
                    }
                }
            }
        }