private void SaveTable_text_Click(object sender, RoutedEventArgs e) { try { SaveFileDialog saveFileDialog = new SaveFileDialog { Filter = "Text files (*.txt)|*.txt" }; if (saveFileDialog.ShowDialog() == true) { using (TextWriter datatotxt = new StreamWriter(saveFileDialog.FileName, false)) { TestTable.SelectAllCells(); TestTable.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader; ApplicationCommands.Copy.Execute(null, TestTable); TestTable.UnselectAllCells(); datatotxt.Write((string)Clipboard.GetData(DataFormats.CommaSeparatedValue)); } } } catch (Exception) { } }