// Save As method private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) { string filePathName = null; using (SaveFileDialog saveFile = new SaveFileDialog()) { saveFile.Title = "Save File As"; saveFile.Filter = "CSV (Comma delimited) (*.csv) | *.csv; *,csv | Excel Workbook (*.xlsx) | *.xlsx; *,xlsx"; saveFile.RestoreDirectory = true; if (saveFile.ShowDialog() == DialogResult.OK) { filePathName = saveFile.FileName; WholeDataFile.Sort(DataImport.SortNameAscending()); if (saveFile.FilterIndex == 1) { Methods.ExportToCsv(WholeDataFile, filePathName); } else if (saveFile.FilterIndex == 2) { Methods.ExportToExcel(WholeDataFile /*((IEnumerable)defaultDgv.DataSource).Cast<DataImport>().ToList()*/, filePathName); } } } }
// Add Data Event private void addDataToolStripMenuItem_Click(object sender, EventArgs e) { filePath = Methods.OpenFile(); if (!string.IsNullOrEmpty(filePath)) { int countDataPointsBefore = WholeDataFile.Count; List <DataImport> dataToAdd = Methods.ImportCSVFile(filePath); if (dataToAdd.Count != 0) { WholeDataFile = Methods.AddDataToList(WholeDataFile, dataToAdd); WholeDataFile.Sort(DataImport.SortNameAscending()); defaultDgv.DataSource = null; defaultDgv.DataSource = WholeDataFile; lblCountPoints.Text = WholeDataFile.Count.ToString() + " data points"; string message = string.Format("{0} data points have been added", dataToAdd.Count); if (WholeDataFile.Count != countDataPointsBefore + dataToAdd.Count) { int countDataPointsAdded = WholeDataFile.Count - countDataPointsBefore; int countConflicts = (countDataPointsBefore + dataToAdd.Count) - WholeDataFile.Count; message = string.Format("{0} data points have been added. {1} data points in conflict have been removed.", countDataPointsAdded.ToString(), countConflicts.ToString()); } MessageBox.Show(message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }
// Ascending Order Sorting private void ascendingToolStripMenuItem_Click(object sender, EventArgs e) { if (defaultDgv.Visible) { defaultDgv.DataSource = null; WholeDataFile.Sort(DataImport.SortNameAscending()); defaultDgv.DataSource = WholeDataFile; } else { //List<DataImport> tempList = //((IEnumerable)editingDgv.DataSource).Cast<DataImport>().ToList(); editingDgv.DataSource = null; Methods.LinesCollection[int.Parse(bindNavig.PositionItem.Text)].Sort(DataImport.SortNameAscending()); editingDgv.DataSource = Methods.LinesCollection[int.Parse(bindNavig.PositionItem.Text)]; } }
public static Dictionary <int, List <DataImport> > GetCollectionOfLines(List <DataImport> originList) { int trackIndex = 0; Dictionary <int, List <DataImport> > collectionList = new Dictionary <int, List <DataImport> >(); originList.Sort(DataImport.SortNameAscending()); List <DataImport> tempList = new List <DataImport>(); int index = 1; while (trackIndex < originList.Count) { tempList = FilterByName(trackIndex, originList); collectionList.Add(index, tempList); trackIndex += tempList.Count; index++; } return(collectionList); }
// 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; }
// Save File Event private void saveToolStripMenuItem_Click(object sender, EventArgs e) { WholeDataFile.Sort(DataImport.SortNameAscending()); Methods.ExportToCsv(WholeDataFile, filePath); }