private void ExportToCsv_Click(object sender, RoutedEventArgs e) { try { DataTable resultDt = ((DataView)datGrid.ItemsSource).ToTable(); SaveFileDialog saveFileDlg = new SaveFileDialog(); saveFileDlg.Filter = "csv files (*.csv)|*.csv"; saveFileDlg.DefaultExt = "csv"; saveFileDlg.FilterIndex = 2; saveFileDlg.RestoreDirectory = true; bool?result = saveFileDlg.ShowDialog(); if (result == true) { var exporter = new CsvDataExporter(); using (var streamWriter = File.OpenWrite(saveFileDlg.FileName)) exporter.Export(resultDt.CreateDataReader(), streamWriter); } } catch (Exception error) { //if some error occurs just show the error message MessageBox.Show(error.Message); } }
private void btnExportCsv_Click(object sender, EventArgs e) { try { //Save CSV file using (SaveFileDialog saveFileDialog = new SaveFileDialog()) { saveFileDialog.Filter = "csv files (*.csv)|*.csv"; saveFileDialog.FilterIndex = 2; saveFileDialog.RestoreDirectory = true; if (saveFileDialog.ShowDialog(this) == DialogResult.OK) { var exporter = new CsvDataExporter(); using (var streamWriter = File.OpenWrite(saveFileDialog.FileName)) exporter.Export(ResultDS.CreateDataReader(ResultDS.Tables.Cast <DataTable>().ToArray()), streamWriter); } } } catch (Exception error) { //if some error occurs just show the error message MessageBox.Show(error.Message); } }