-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataGridViewExt.cs
More file actions
38 lines (36 loc) · 1.13 KB
/
DataGridViewExt.cs
File metadata and controls
38 lines (36 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SimpleRoutingAnalyzer
{
static class DataGridViewExt
{
public static void ExportCSV(this DataGridView dgv, string path)
{
using (var file = File.CreateText(path))
{
var line = new StringBuilder();
foreach (DataGridViewColumn column in dgv.Columns)
{
if (line.Length > 0) line.Append(",");
line.Append(column.HeaderText);
}
file.WriteLine(line.ToString());
foreach (DataGridViewRow row in dgv.Rows)
{
line.Clear();
foreach (DataGridViewCell cell in row.Cells)
{
if (line.Length > 0) line.Append(",");
line.Append(cell.Value?.ToString() ?? "null");
}
file.WriteLine(line.ToString());
}
}
}
}
}