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; } } }
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); } }