Example #1
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 #2
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());
                    }
                }
            }
        }