//add buttom clicked private void AddButton_Click(object sender, EventArgs e) { string ItemName = this.itemName.Text.Trim(); string Category = category.Text.Trim(); try { double Price = Convert.ToDouble(price.Text); if (ItemName != null && ItemName != "" && Category != null && Category != "" && Price >= 0) { int RowIndex = helper.GetEmptyRowIndex(RecordsGridView); //create new instance of ItemRecords itemRecords = new ItemRecords(ItemName, Category, Price); itemRecords.AddToRecords(RowIndex, RecordsGridView); helper.ClearFields(itemName, price); MessageBox.Show("Record has successfully been added.", "Records Added", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("Please fill up all fields properly.", "Fields Invalid", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (FormatException) { MessageBox.Show("Please enter a valid Price.", "Invalid Price", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
//import button clicked private void ImportButton_Click(object sender, EventArgs e) { try { if (fileLocation.Text != null && fileLocation.Text != "") { TextFieldParser csvceader = new TextFieldParser(fileLocation.Text); csvceader.SetDelimiters(new string[] { "," }); while (!csvceader.EndOfData) { string[] rowdata = csvceader.ReadFields(); //create new instance of itemRecords and pass row index and grid to AddToRecords itemRecords = new ItemRecords(rowdata[0], rowdata[1], (Convert.ToDouble(rowdata[2]))); int rowIndex = helper.GetEmptyRowIndex(RecordsGridView); itemRecords.AddToRecords(rowIndex, RecordsGridView); helper.ClearFields(fileLocation); } MessageBox.Show("Records have successfully been imported.", "Records Imported", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("File location not specified.", "No Filepath selected", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (Exception ex) { MessageBox.Show("One or more records couldn't be imported. " + ex.Message, "Couldn't import record", MessageBoxButtons.OK, MessageBoxIcon.Error); } }