質問編集履歴
4
画像の追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -103,6 +103,7 @@
|
|
103
103
|
```
|
104
104
|

|
105
105
|

|
106
|
+

|
106
107
|
###該当のソースコード
|
107
108
|
```C#
|
108
109
|
/// <summary>
|
3
画像の追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -102,6 +102,7 @@
|
|
102
102
|
(... as System.Collections.Generic.List<KMAP.AirPortDataClass>) が null を返しました。
|
103
103
|
```
|
104
104
|

|
105
|
+

|
105
106
|
###該当のソースコード
|
106
107
|
```C#
|
107
108
|
/// <summary>
|
2
画像の追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -101,7 +101,7 @@
|
|
101
101
|
|
102
102
|
(... as System.Collections.Generic.List<KMAP.AirPortDataClass>) が null を返しました。
|
103
103
|
```
|
104
|
-
|
104
|
+

|
105
105
|
###該当のソースコード
|
106
106
|
```C#
|
107
107
|
/// <summary>
|
1
動画に書かれているコードを追加しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -10,7 +10,90 @@
|
|
10
10
|
|
11
11
|
###参考にしたもの
|
12
12
|
[C# Tutorial - Read & Write csv file | FoxLearn](https://www.youtube.com/watch?v=96kHaIUMTEk)
|
13
|
+
写経元は以下のようになっています。
|
14
|
+
Studentクラスはデータグリッドビューのデータソースの選択から指定しています。
|
15
|
+
```
|
16
|
+
using CsvHelper;
|
17
|
+
using System;
|
18
|
+
using System.Collections.Generic;
|
19
|
+
using System.ComponentModel;
|
20
|
+
using System.Data;
|
21
|
+
using System.Drawing;
|
22
|
+
using System.IO;
|
23
|
+
using System.Linq;
|
24
|
+
using System.Text;
|
25
|
+
using System.Threading.Tasks;
|
26
|
+
using System.Windows.Forms;
|
13
27
|
|
28
|
+
namespace ReadWriteCSV
|
29
|
+
{
|
30
|
+
public partial class Form1 : Form
|
31
|
+
{
|
32
|
+
public Form1()
|
33
|
+
{
|
34
|
+
InitializeComponent();
|
35
|
+
}
|
36
|
+
|
37
|
+
private void btnWrite_Click(object sender, EventArgs e)
|
38
|
+
{
|
39
|
+
using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "CSV|*.csv", ValidateNames = true })
|
40
|
+
{
|
41
|
+
if (sfd.ShowDialog()==DialogResult.OK)
|
42
|
+
{
|
43
|
+
using (var sw = new StreamWriter(sfd.FileName))
|
44
|
+
{
|
45
|
+
var writer = new CsvWriter(sw);
|
46
|
+
writer.WriteHeader(typeof(Student));
|
47
|
+
foreach(Student s in studentBindingSource.DataSource as List<Student>)
|
48
|
+
{
|
49
|
+
writer.WriteRecord(s);
|
50
|
+
}
|
51
|
+
}
|
52
|
+
MessageBox.Show("Your data has been successfully saved.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
53
|
+
}
|
54
|
+
}
|
55
|
+
}
|
56
|
+
|
57
|
+
private void Form1_Load(object sender, EventArgs e)
|
58
|
+
{
|
59
|
+
studentBindingSource.DataSource = new List<Student>();
|
60
|
+
}
|
61
|
+
|
62
|
+
private void btnRead_Click(object sender, EventArgs e)
|
63
|
+
{
|
64
|
+
using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "CSV|*.csv", ValidateNames = true })
|
65
|
+
{
|
66
|
+
if (ofd.ShowDialog()==DialogResult.OK)
|
67
|
+
{
|
68
|
+
var sr = new StreamReader(new FileStream(ofd.FileName, FileMode.Open));
|
69
|
+
var csv = new CsvReader(sr);
|
70
|
+
//studentBindingSource.DataSource = csv.GetRecord<Student>().ToString();
|
71
|
+
studentBindingSource.DataSource = csv.GetRecords<Student>();
|
72
|
+
}
|
73
|
+
}
|
74
|
+
}
|
75
|
+
}
|
76
|
+
}
|
77
|
+
```
|
78
|
+
```
|
79
|
+
using System;
|
80
|
+
using System.Collections.Generic;
|
81
|
+
using System.Linq;
|
82
|
+
using System.Text;
|
83
|
+
using System.Threading.Tasks;
|
84
|
+
|
85
|
+
namespace ReadWriteCSV
|
86
|
+
{
|
87
|
+
public class Student
|
88
|
+
{
|
89
|
+
public string StudentID { get; set; }
|
90
|
+
public string StudentName { get; set; }
|
91
|
+
public string Email { get; set; }
|
92
|
+
public string Phone { get; set; }
|
93
|
+
}
|
94
|
+
}
|
95
|
+
|
96
|
+
```
|
14
97
|
###発生している問題・エラーメッセージ
|
15
98
|
foreachの行で発生。
|
16
99
|
```
|