Example #1
0
        private void buttonSave_Click(object sender, EventArgs e)
        {
            var userInputFörfattareInfo = new string[dataGridViewFörfattare.Rows.Count];
            var userInputBokInfo        = new string[dataGridViewBok.Rows.Count];

            for (var i = 1; i < dataGridViewFörfattare.Rows.Count; i++)
            {
                userInputFörfattareInfo[i - 1] = dataGridViewFörfattare.Rows[i - 1].Cells["Input"].Value.ToString();
            }

            for (var i = 0; i < dataGridViewBok.Rows.Count; i++)
            {
                userInputBokInfo[i] = dataGridViewBok.Rows[i].Cells["Input"].Value.ToString();
            }


            var inputFörlag = dataGridViewFörfattare.Rows[3].Cells["Input"].Value as Förlag;

            var nyFörfattare = EntityAdder.AddNyFörfattare(userInputFörfattareInfo);

            Db.Författare.Add(nyFörfattare);
            Db.Böcker.Add(EntityAdder.AddNyBok(userInputBokInfo));
            Db.SaveChanges();

            Db.FörfattareBöckerFörlags.Add(EntityAdder.AddNyFörfattareBöckerFörlag(userInputBokInfo, inputFörlag, nyFörfattare));
            Db.SaveChanges();

            Close();
        }
Example #2
0
        private void dataGridViewBok_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            buttonSave.Enabled = false;

            switch (e.RowIndex)
            {
            case 0:     // isbn
                if (dataGridViewBok.Rows[0].Cells["Input"].Value == null ||
                    dataGridViewBok.Rows[0].Cells["Input"].Value?.ToString().Length != 13 ||
                    !EntityAdder.ISBNConstraint.IsMatch(dataGridViewBok.Rows[0].Cells["Input"].Value?.ToString()))
                {
                    MessageBox.Show("ISBN format is not correct, must be 13 numbers.", "Error");
                    dataGridViewBok.Rows[0].Cells["Input"].Value = null;
                }
                else if (!EntityAdder.IsISBNUnique(dataGridViewBok.Rows[0].Cells["Input"].Value?.ToString(), MainForm.GetISBNList))
                {
                    MessageBox.Show("A book with that ISBN already exists!", "Error");
                    dataGridViewBok.Rows[0].Cells["Input"].Value = null;
                }
                break;

            case 3:     // pris
                if (!decimal.TryParse(dataGridViewBok.Rows[3].Cells["Input"].Value?.ToString(), out decimal priceResult))
                {
                    MessageBox.Show("Price format is not correct, can only be numbers", "Error");
                    dataGridViewBok.Rows[3].Cells["Input"].Value = "";
                }
                break;

            case 4:     // utgivningsdatum
                if (!DateTime.TryParse(dataGridViewBok.Rows[4].Cells["Input"].Value?.ToString(), out DateTime dateTimeResult))
                {
                    MessageBox.Show("Date format is not correct, use yy-mm-dd", "Error");
                    dataGridViewBok.Rows[4].Cells["Input"].Value = "";
                }
                break;

            default:
                break;
            }

            if (!DateTime.TryParse(dataGridViewBok.Rows[4].Cells["Input"].Value?.ToString(), out DateTime dateTimeResult2))
            {
                return;                                                                                                             //if last cell isn't populated, R E T U R N
            }
            EnableSaveButton();
        }
Example #3
0
        private void treeViewCustomerOrders_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            if (isFörfattare)
            {
                Förlag förlag           = null;
                var    userInputBokInfo = new string[5];

                if ((dataGridView.Rows.Count - AmountOfRows) < 1)
                {
                    return;
                }


                for (var j = AmountOfRows; j < dataGridView.Rows.Count; j++)
                {
                    for (var x = 0; x < 5; x++)
                    {
                        if (string.IsNullOrEmpty(dataGridView.Rows[j].Cells[x].Value as string))
                        {
                            break;
                        }


                        userInputBokInfo[x] = dataGridView.Rows[j].Cells[x].Value.ToString();
                    }
                    if (!userInputBokInfo.Contains(null))
                    {
                        förlag = dataGridView.Rows[j].Cells["Förlag"].Value as Förlag;
                        db.Böcker.Add(EntityAdder.AddNyBok(userInputBokInfo));
                        db.SaveChanges();

                        db.FörfattareBöckerFörlags.Add(EntityAdder.AddNyFörfattareBöckerFörlag(userInputBokInfo, förlag, activeFörfattare));
                        db.SaveChanges();
                    }
                }
            }
        }
Example #4
0
 void addEntity(List <mEntity> list, string folderName)
 {
     EntityAdder.AddEntity(list, folderName);
 }
Example #5
0
        private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
            {
                return;
            }

            if (isButik)
            {
                AddBookToStore(e);
            }
            else if (isFörfattare)
            {   //check if any of the datagrid rows isbn are duplicate
                for (int i = 0; i < dataGridView.Rows.Count; i++)
                {
                    if (dataGridView.Rows[i].Cells["ISBN"].Value == null)
                    {
                        continue;
                    }

                    var currentISBN = dataGridView.Rows[i].Cells["ISBN"].Value?.ToString();
                    for (int j = i + 1; j < dataGridView.Rows.Count; j++)
                    {
                        if (currentISBN != dataGridView.Rows[j].Cells["ISBN"].Value?.ToString())
                        {
                            continue;
                        }
                        MessageBox.Show("A book with that ISBN already exists!", "Error");
                        dataGridView.Rows[j].Cells["ISBN"].Value = null;
                        return;
                    }
                }

                switch (e.ColumnIndex)
                {
                case 0:     // isbn
                    if (dataGridView.Rows[e.RowIndex].Cells["ISBN"].Value == null ||
                        dataGridView.Rows[e.RowIndex].Cells["ISBN"].Value?.ToString().Length != 13 ||
                        !EntityAdder.ISBNConstraint.IsMatch(dataGridView.Rows[e.RowIndex].Cells["ISBN"].Value?.ToString()))
                    {
                        MessageBox.Show("ISBN format is not correct, must be 13 numbers.", "Error");
                        dataGridView.Rows[e.RowIndex].Cells["ISBN"].Value = null;
                        return;
                    }
                    else if (!EntityAdder.IsISBNUnique(dataGridView.Rows[e.RowIndex].Cells["ISBN"].Value?.ToString(), GetISBNList) &&
                             e.RowIndex >= AmountOfRows)
                    {
                        MessageBox.Show("A book with that ISBN already exists!", "Error");
                        dataGridView.Rows[e.RowIndex].Cells["ISBN"].Value = null;
                        return;
                    }
                    break;

                case 3:     // pris
                    if (!decimal.TryParse(dataGridView.Rows[e.RowIndex].Cells["Pris"].Value?.ToString(), out decimal priceResult))
                    {
                        MessageBox.Show("Price format is not correct, can only be numbers", "Error");
                        dataGridView.Rows[e.RowIndex].Cells["Pris"].Value = "";
                        return;
                    }
                    break;

                case 4:     // utgivningsdatum
                    if (!DateTime.TryParse(dataGridView.Rows[e.RowIndex].Cells["Utgivningsdatum"].Value?.ToString(), out DateTime dateTimeResult))
                    {
                        MessageBox.Show("Date format is not correct, use yy-mm-dd", "Error");
                        dataGridView.Rows[e.RowIndex].Cells["Utgivningsdatum"].Value = "";
                        return;
                    }
                    break;

                default:
                    break;
                }


                if (dataGridView.Rows[e.RowIndex].Tag is Böcker bok) //isbn, språk, pris, utgivningsdatum, förlag
                {
                    if (!böcker.Contains(bok))
                    {
                        return;
                    }

                    if (dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex] == dataGridView.Rows[e.RowIndex].Cells["ISBN"] ||
                        dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex] == dataGridView.Rows[e.RowIndex].Cells["Förlag"])
                    {
                        var userInputBokInfo = new string[5];

                        for (var i = 0; i < 5; i++)
                        {
                            if (string.IsNullOrEmpty(dataGridView.Rows[e.RowIndex].Cells[i].Value as string))
                            {
                                return;
                            }

                            userInputBokInfo[i] = dataGridView.Rows[e.RowIndex].Cells[i].Value.ToString();
                        }

                        var förlag = dataGridView.Rows[e.RowIndex].Cells["Förlag"].Value as Förlag;

                        db.Böcker.Remove(bok);
                        db.SaveChanges();
                        db.Böcker.Add(EntityAdder.AddNyBok(userInputBokInfo));
                        db.SaveChanges();

                        db.FörfattareBöckerFörlags.Add(EntityAdder.AddNyFörfattareBöckerFörlag(userInputBokInfo, förlag, activeFörfattare));
                        db.SaveChanges();
                    }
                    else
                    {
                        bok.Titel           = dataGridView.Rows[e.RowIndex].Cells["Titel"].Value?.ToString();
                        bok.Språk           = dataGridView.Rows[e.RowIndex].Cells["Språk"].Value?.ToString();
                        bok.Pris            = decimal.Parse(dataGridView.Rows[e.RowIndex].Cells["Pris"].Value.ToString());
                        bok.Utgivningsdatum = DateTime.Parse(dataGridView.Rows[e.RowIndex].Cells["Utgivningsdatum"].Value?.ToString());
                    }
                }
            }
        }