Index: src/Common/DelftTools.Controls.Swf/Table/TableViewCsvImportWizardDialog.cs =================================================================== diff -u -r8f6ae890fed8e8eae3a32f9c0498a10f82e0ddf9 -r5fc71a385897af92ccb092f2f969b5709afab85a --- src/Common/DelftTools.Controls.Swf/Table/TableViewCsvImportWizardDialog.cs (.../TableViewCsvImportWizardDialog.cs) (revision 8f6ae890fed8e8eae3a32f9c0498a10f82e0ddf9) +++ src/Common/DelftTools.Controls.Swf/Table/TableViewCsvImportWizardDialog.cs (.../TableViewCsvImportWizardDialog.cs) (revision 5fc71a385897af92ccb092f2f969b5709afab85a) @@ -13,13 +13,12 @@ { public class TableViewCsvImportWizardDialog : CsvImportWizardDialog { + private static readonly ILog Log = LogManager.GetLogger(typeof(TableViewCsvImportWizardDialog)); private readonly TableView tableView; private readonly bool importFromClipboard; private readonly string clipboardText; - private static readonly ILog Log = LogManager.GetLogger(typeof(TableViewCsvImportWizardDialog)); - - public TableViewCsvImportWizardDialog(TableView tableView, string clipboardText=null) + public TableViewCsvImportWizardDialog(TableView tableView, string clipboardText = null) { this.tableView = tableView; importFromClipboard = !string.IsNullOrEmpty(clipboardText); @@ -31,7 +30,7 @@ // we're importing from file; don't bother with file select: var filePage = Pages.OfType().First(); RemovePage(filePage); - + // prepare the preview: var csvSeparatorPage = Pages.OfType().First(); using (var reader = new StringReader(clipboardText)) @@ -42,57 +41,50 @@ while ((line = reader.ReadLine()) != null) { if (++numLines > 30) + { break; + } first30Lines.AppendLine(line); } csvSeparatorPage.PreviewText = first30Lines.ToString(); } } } - - protected override IEnumerable GetRequiredFields() - { - return - tableView.Columns.Where(c => c.Visible) - .Select(column => new CsvRequiredField(column.Name, column.ColumnType)); - } - protected override void OnUserFinishedMapping(string filePath, CsvMappingData mappingData) + public void DoImport(CsvMappingData mappingData, string filePath = null) { - DoImport(mappingData, filePath); - } - - public void DoImport(CsvMappingData mappingData, string filePath=null) - { var importer = new CsvImporter(); - using (var stream = importFromClipboard ? (Stream)new MemoryStream(Encoding.UTF8.GetBytes(clipboardText)): File.OpenRead(filePath)) - using (var streamReader = new StreamReader(stream)) + using (var stream = importFromClipboard ? (Stream) new MemoryStream(Encoding.UTF8.GetBytes(clipboardText)) : File.OpenRead(filePath)) { - var importedData = importer.SplitToTable(streamReader, mappingData.Settings); - importedData = importer.Extract(importedData, mappingData.FieldToColumnMapping, mappingData.Filters); + using (var streamReader = new StreamReader(stream)) + { + var importedData = importer.SplitToTable(streamReader, mappingData.Settings); + importedData = importer.Extract(importedData, mappingData.FieldToColumnMapping, mappingData.Filters); - // delete all existing data - tableView.SelectRows(Enumerable.Range(0, tableView.RowCount).ToArray()); - tableView.DeleteCurrentSelection(); + // delete all existing data + tableView.SelectRows(Enumerable.Range(0, tableView.RowCount).ToArray()); + tableView.DeleteCurrentSelection(); - var exceptionMode = tableView.ExceptionMode; - tableView.ExceptionMode = TableView.ValidationExceptionMode.ThrowException; //throw exception + var exceptionMode = tableView.ExceptionMode; + tableView.ExceptionMode = TableView.ValidationExceptionMode.ThrowException; //throw exception - try - { - tableView.BeginInit(); - TableView.DoActionInEditAction( - tableView, importFromClipboard ? "Pasting values" : "Importing values", - () => + try + { + tableView.BeginInit(); + TableView.DoActionInEditAction( + tableView, importFromClipboard ? "Pasting values" : "Importing values", + () => { // start importing data int rowIndex = 0; bool skipNextAdd = false; foreach (DataRow row in importedData.Rows) { if (!skipNextAdd) + { tableView.AddNewRowToDataSource(); - + } + skipNextAdd = false; bool success; var message = ""; @@ -116,13 +108,26 @@ rowIndex++; } }); - tableView.EndInit(); + tableView.EndInit(); + } + finally + { + tableView.ExceptionMode = exceptionMode; + } } - finally - { - tableView.ExceptionMode = exceptionMode; - } } } + + protected override IEnumerable GetRequiredFields() + { + return + tableView.Columns.Where(c => c.Visible) + .Select(column => new CsvRequiredField(column.Name, column.ColumnType)); + } + + protected override void OnUserFinishedMapping(string filePath, CsvMappingData mappingData) + { + DoImport(mappingData, filePath); + } } } \ No newline at end of file