×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: C#
Posted by: Spiro Bad
Added: Feb 19, 2017 7:13 PM
Views: 2259
  1. void SaveDataGridViewToCSV(string filename, DataGridView grd_Data)
  2.         {
  3.             // Choose whether to write header. Use EnableWithoutHeaderText instead to omit header.
  4.             grd_Data.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
  5.             // Select all the cells
  6.             grd_Data.SelectAll();
  7.             // Copy selected cells to DataObject
  8.             DataObject dataObject = grd_Data.GetClipboardContent();
  9.             // Get the text of the DataObject, and serialize it to a file
  10.             File.WriteAllText(filename, dataObject.GetText(TextDataFormat.CommaSeparatedValue));
  11.             Application.DoEvents();
  12.         }