private void openToolStripMenuItem_Click(object sender, EventArgs e) { var ofd = new OpenFileDialog { Filter = "csv files (*.csv)|*.csv|txt files (*.txt)|*.txt|All files (*.*)|*.*", FilterIndex = 0, RestoreDirectory = true }; if(ofd.ShowDialog() == DialogResult.OK) { _currentCsv = new CsvFile(ofd.FileName); dataGridView_Main.DataSource = _currentCsv.DataSource; } }
public static CsvFile CreateCsvObject(StringBuilder sourceData, string delimiter, out bool loadRetVal) { using(var ms = new MemoryStream()) { using(var sw = new StreamWriter(ms, Encoding.UTF8)) { sw.Write(sourceData.ToString()); sw.Flush(); ms.Seek(0, SeekOrigin.Begin); var csv = new CsvFile(new CsvProperties { Delimiter = delimiter }); loadRetVal = csv.Load(ms); return csv; } } }
public static string DetermineDelimiter(string filename, out int numColumns) { var delimiters = new[] { "\t", ",", ";", "|" }; foreach(var delimiter in delimiters) { using(var fs = new FileStream(filename, FileMode.Open, FileAccess.Read)) { using(var csv = new CsvFile(new CsvProperties { Delimiter = delimiter, MaxRowCount = 5 })) { if(csv.LoadCsvFile(fs) && csv.DataSource.Columns.Count > 1) { numColumns = csv.DataSource.Columns.Count; return delimiter; } } } } numColumns = 0; return CsvProperties.DefaultDelimiter; }
private void OpenFile(string filename) { try { var frmCsvProperties = new Frm_CsvProperties(this, false); int numColumns; string suspectedDelimiter = CsvFile.DetermineDelimiter(filename, out numColumns); frmCsvProperties.CsvProperties.Delimiter = suspectedDelimiter; frmCsvProperties.CsvProperties.NumColumns = numColumns; if(frmCsvProperties.ShowDialog() == DialogResult.OK) { _currentCsv = new CsvFile(frmCsvProperties.CsvProperties); if(_currentCsv.Load(filename)) { if(_currentCsv.LoadErrors.Count > 0) { var frmErrors = new Frm_Errors("File has been loaded, but the following errors were found:", _currentCsv.LoadErrors); frmErrors.ShowDialog(); } dataGridView_Main.DataSource = _currentCsv.DataSource; _modified = false; UpdateMenuStates(); UpdateStatusBar(); CurrentFilename = filename; } else { if(_currentCsv.LoadErrors.Count > 0) { var frmErrors = new Frm_Errors("File could not be loaded. The following errors were found:", _currentCsv.LoadErrors); frmErrors.ShowDialog(); } else { MessageBox.Show("Could not load file, as it appears to be invalid"); } _currentCsv = null; } } } catch(Exception) { _currentCsv = null; MessageBox.Show("Failed to open file - is it in use by another process?", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
private void NewCsvFile() { var frmProperties = new Frm_CsvProperties(this, false, new CsvProperties { NumColumns = 10, NumRows = 10 }); if(frmProperties.ShowDialog() == DialogResult.OK) { _currentCsv = new CsvFile(frmProperties.CsvProperties); _currentCsv.CreateNew(frmProperties.CsvProperties.NumColumns, frmProperties.CsvProperties.NumRows); dataGridView_Main.DataSource = _currentCsv.DataSource; _modified = false; UpdateMenuStates(); UpdateStatusBar(); CurrentFilename = null; } }
private void closeToolStripMenuItem1_Click(object sender, EventArgs e) { if(CheckForChanges()) { _currentCsv = null; CurrentFilename = null; dataGridView_Main.DataSource = null; _modified = false; UpdateMenuStates(); UpdateStatusBar(); } }