// Check out all the missing data inside a single already filtered line based on the name property // Summary: // In the single line visualized on dataGridView, check first if the first element of line // is equal to 1 (the side on the right of dot in the name property), // if it's not it asks to user if wants to add the first element. Then asks to the user // if the last element is the correct one. And the finally adds all missing elements // inside the line based on succesive numbers from first element to the last. // Parameters: // dataGridView that represents the current visualization on the application // boolean variable that allows to switch the operation whether the line is in ascending order // or in descending order // Returns: // None public static void CheckMissingDataInSingleLine(DataGridView dgv, int indexOfLine) { DialogResult result; BindingSource bs = new BindingSource(); bs.DataSource = dgv.DataSource; dgv.DataSource = bs; LinesCollection[indexOfLine].Sort(DataImport.SortNameAscending()); List <DataImport> viewLine = LinesCollection[indexOfLine]; bs.DataSource = viewLine; int firstElement = int.Parse(viewLine.First().EditNameProperty()[1]); int lastElement = int.Parse(viewLine.Last().EditNameProperty()[1]); int countMissingPoints = 0; if (firstElement != 1) { result = CustomMessageForm.CustomMessageBox.Show("Data Check", "Missing the first data point of line. Would you like to add it?", "Yes", "No"); if (result == DialogResult.Yes) { for (int i = 1; i < firstElement; i++) { bs.Insert(i - 1, new DataImport(viewLine.First().EditNameProperty()[0] + "." + i.ToString(), 0, 0, 0, null, null, null)); countMissingPoints++; dgv.Rows[i - 1].DefaultCellStyle.BackColor = System.Drawing.Color.Aquamarine; } } } result = CustomMessageForm.CustomMessageBox.Show("Data Check", string.Format("The last data point is {0}. Would you like to add more data points?", (viewLine.Last().EditNameProperty()[0] + "." + lastElement.ToString())), "Yes", "No"); if (result == DialogResult.Yes) { DialogResult resText; int lastPoint; bool checkLastPoint; do { InsertTextForm insForm = new InsertTextForm("Insert Data", "Insert the actual last point of line"); resText = insForm.Show(); checkLastPoint = int.TryParse(insForm.InputData, out lastPoint); }while (checkLastPoint == false); if (resText == DialogResult.OK) { for (int i = lastElement + 1; i <= lastPoint; i++) { DataImport tempData = new DataImport((viewLine.First().EditNameProperty()[0] + "." + i.ToString()), 0, 0, 0, null, null, null); bs.Add(tempData); countMissingPoints++; dgv.Rows[viewLine.Count - 1].DefaultCellStyle.BackColor = System.Drawing.Color.Aquamarine; } } } for (int i = 0; i < viewLine.Count - 1; i++) { if ((int.Parse(viewLine[i + 1].EditNameProperty()[1]) - int.Parse(viewLine[i].EditNameProperty()[1])) != 1) { bs.Insert(i + 1, new DataImport((viewLine[i].EditNameProperty()[0] + "." + (int.Parse(viewLine[i].EditNameProperty()[1]) + 1).ToString()), 0, 0, 0, null, null, null)); countMissingPoints++; dgv.Rows[i + 1].DefaultCellStyle.BackColor = System.Drawing.Color.Aquamarine; } } MessageBox.Show(string.Format("Added {0} data points", countMissingPoints), "Data Check", MessageBoxButtons.OK, MessageBoxIcon.Information); bs.DataSource = null; dgv.DataSource = viewLine; LinesCollection[indexOfLine] = viewLine; }
// Import Csv file using Csv helpers library // Summary: // create a stream reader variable to instantiate a csv reader // while the csv reader is reading the file it extracts the fields // and validating the fields and adding the good records to the list to export // Parameters: // string of path of file to import in the application // Returns: // collection of records of data import type public static List <DataImport> ImportCSVFile(string filePath) { List <DataImport> output = new List <DataImport>(); string message = "Cannot open file. File doesn't seem to be in the correct format."; if (filePath != null) { using (StreamReader reader = new StreamReader(filePath)) { using (var csv = new CsvReader(reader)) { try { csv.Configuration.HasHeaderRecord = false; while (csv.Read()) { csv.Configuration.SkipEmptyRecords = true; double easting; double northing; double elevation; string code = string.Empty; string description1 = string.Empty; string description2 = string.Empty; //if the file doesn't not contain easting, northing and elevation // throw an exception if (csv.CurrentRecord.Length < 4) { message = "Cannot Open File. Important Data is missing."; throw new FormatException(); } string name = csv.GetField(0); bool checkEasting = csv.TryGetField(1, out easting); bool checkNorthing = csv.TryGetField(2, out northing); bool checkElevation = csv.TryGetField(3, out elevation); if (csv.CurrentRecord.Length >= 7) { code = csv.GetField(4); description1 = csv.GetField(5); description2 = csv.GetField(6); } else if (csv.CurrentRecord.Length >= 6) { code = csv.GetField(4); description1 = csv.GetField(5); } else if (csv.CurrentRecord.Length >= 5) { code = csv.GetField(4); } // Check if there is header and do not add it to the list if (csv.Row == 1) { if (!string.IsNullOrEmpty(name) && checkEasting == false && checkElevation == false && checkNorthing == false && !string.IsNullOrEmpty(code) && !string.IsNullOrEmpty(description1) && !string.IsNullOrEmpty(description2)) { name = null; } } // if the conversion of easting, northing and elevation fails throw an exception // It means something wrong in the origin file or not the proper file else if (checkEasting == false || checkElevation == false || checkNorthing == false) { message = "Cannot Open File. File not formatted correctly or corrupted."; throw new FormatException(); } // Skip eventually record that contains PRS. It's not data if (!string.IsNullOrEmpty(name) && !name.Contains("PRS") && name.Length <= 10 && checkEasting && checkNorthing && checkElevation) { DataImport data = new DataImport(name, easting, northing, elevation, code, description1, description2); output.Add(data); } } } catch (FormatException) { MessageBox.Show(message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception) { MessageBox.Show("Cannot Open file. Something went wrong!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } } return(output); }
// Save File Event private void saveToolStripMenuItem_Click(object sender, EventArgs e) { WholeDataFile.Sort(DataImport.SortNameAscending()); Methods.ExportToCsv(WholeDataFile, filePath); }