質問編集履歴
4
画像の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -208,6 +208,8 @@
|
|
208
208
|
|
209
209
|
![イメージ説明](367f415ecb387befab71cbf5a09df901.png)
|
210
210
|
|
211
|
+
![イメージ説明](53a0ca286479901b37d489592c6b85f1.jpeg)
|
212
|
+
|
211
213
|
###該当のソースコード
|
212
214
|
|
213
215
|
```C#
|
3
画像の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -206,6 +206,8 @@
|
|
206
206
|
|
207
207
|
![イメージ説明](c37bf2ab2c6f0f32e37f178ef282bc6c.png)
|
208
208
|
|
209
|
+
![イメージ説明](367f415ecb387befab71cbf5a09df901.png)
|
210
|
+
|
209
211
|
###該当のソースコード
|
210
212
|
|
211
213
|
```C#
|
2
画像の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -204,7 +204,7 @@
|
|
204
204
|
|
205
205
|
```
|
206
206
|
|
207
|
-
|
207
|
+
![イメージ説明](c37bf2ab2c6f0f32e37f178ef282bc6c.png)
|
208
208
|
|
209
209
|
###該当のソースコード
|
210
210
|
|
1
動画に書かれているコードを追加しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -22,7 +22,173 @@
|
|
22
22
|
|
23
23
|
[C# Tutorial - Read & Write csv file | FoxLearn](https://www.youtube.com/watch?v=96kHaIUMTEk)
|
24
24
|
|
25
|
-
|
25
|
+
写経元は以下のようになっています。
|
26
|
+
|
27
|
+
Studentクラスはデータグリッドビューのデータソースの選択から指定しています。
|
28
|
+
|
29
|
+
```
|
30
|
+
|
31
|
+
using CsvHelper;
|
32
|
+
|
33
|
+
using System;
|
34
|
+
|
35
|
+
using System.Collections.Generic;
|
36
|
+
|
37
|
+
using System.ComponentModel;
|
38
|
+
|
39
|
+
using System.Data;
|
40
|
+
|
41
|
+
using System.Drawing;
|
42
|
+
|
43
|
+
using System.IO;
|
44
|
+
|
45
|
+
using System.Linq;
|
46
|
+
|
47
|
+
using System.Text;
|
48
|
+
|
49
|
+
using System.Threading.Tasks;
|
50
|
+
|
51
|
+
using System.Windows.Forms;
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
namespace ReadWriteCSV
|
56
|
+
|
57
|
+
{
|
58
|
+
|
59
|
+
public partial class Form1 : Form
|
60
|
+
|
61
|
+
{
|
62
|
+
|
63
|
+
public Form1()
|
64
|
+
|
65
|
+
{
|
66
|
+
|
67
|
+
InitializeComponent();
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
private void btnWrite_Click(object sender, EventArgs e)
|
74
|
+
|
75
|
+
{
|
76
|
+
|
77
|
+
using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "CSV|*.csv", ValidateNames = true })
|
78
|
+
|
79
|
+
{
|
80
|
+
|
81
|
+
if (sfd.ShowDialog()==DialogResult.OK)
|
82
|
+
|
83
|
+
{
|
84
|
+
|
85
|
+
using (var sw = new StreamWriter(sfd.FileName))
|
86
|
+
|
87
|
+
{
|
88
|
+
|
89
|
+
var writer = new CsvWriter(sw);
|
90
|
+
|
91
|
+
writer.WriteHeader(typeof(Student));
|
92
|
+
|
93
|
+
foreach(Student s in studentBindingSource.DataSource as List<Student>)
|
94
|
+
|
95
|
+
{
|
96
|
+
|
97
|
+
writer.WriteRecord(s);
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
MessageBox.Show("Your data has been successfully saved.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
104
|
+
|
105
|
+
}
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
private void Form1_Load(object sender, EventArgs e)
|
114
|
+
|
115
|
+
{
|
116
|
+
|
117
|
+
studentBindingSource.DataSource = new List<Student>();
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
private void btnRead_Click(object sender, EventArgs e)
|
124
|
+
|
125
|
+
{
|
126
|
+
|
127
|
+
using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "CSV|*.csv", ValidateNames = true })
|
128
|
+
|
129
|
+
{
|
130
|
+
|
131
|
+
if (ofd.ShowDialog()==DialogResult.OK)
|
132
|
+
|
133
|
+
{
|
134
|
+
|
135
|
+
var sr = new StreamReader(new FileStream(ofd.FileName, FileMode.Open));
|
136
|
+
|
137
|
+
var csv = new CsvReader(sr);
|
138
|
+
|
139
|
+
//studentBindingSource.DataSource = csv.GetRecord<Student>().ToString();
|
140
|
+
|
141
|
+
studentBindingSource.DataSource = csv.GetRecords<Student>();
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
```
|
154
|
+
|
155
|
+
```
|
156
|
+
|
157
|
+
using System;
|
158
|
+
|
159
|
+
using System.Collections.Generic;
|
160
|
+
|
161
|
+
using System.Linq;
|
162
|
+
|
163
|
+
using System.Text;
|
164
|
+
|
165
|
+
using System.Threading.Tasks;
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
namespace ReadWriteCSV
|
170
|
+
|
171
|
+
{
|
172
|
+
|
173
|
+
public class Student
|
174
|
+
|
175
|
+
{
|
176
|
+
|
177
|
+
public string StudentID { get; set; }
|
178
|
+
|
179
|
+
public string StudentName { get; set; }
|
180
|
+
|
181
|
+
public string Email { get; set; }
|
182
|
+
|
183
|
+
public string Phone { get; set; }
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
}
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
```
|
26
192
|
|
27
193
|
###発生している問題・エラーメッセージ
|
28
194
|
|