回答編集履歴

1

追記

2018/12/13 09:28

投稿

退会済みユーザー
test CHANGED
@@ -11,3 +11,199 @@
11
11
 
12
12
 
13
13
  理解できていなかったのでしょうか? それとも List<T> を使う案ではやりたいことができないとか?
14
+
15
+
16
+
17
+ **【追伸】**
18
+
19
+
20
+
21
+ ホントは、質問者さんが先輩に聞くべき話だとは思いますが・・・
22
+
23
+
24
+
25
+ > foreachを省略してカンマ区切りでDataGridViewに表示する方法があれば教えていただきたいです。
26
+
27
+
28
+
29
+ そういう方法はなさそうです。少なくとも自分は知りません。
30
+
31
+
32
+
33
+ なので、質問者さんの前のスレッドで私が提案した List<T> 案を使って、foreach を使わないで済む方法を書いておきます。この案が質問者さんの目的に合わないなら、どこかどう合わないのか具体的に書いてください。
34
+
35
+
36
+
37
+ データベースには Microsoft が提供するサンプルデータベース Northwind の Employees テーブルを使ってみます。以下のような構造で、質問者さんの使っているものに近いのでは?
38
+
39
+
40
+
41
+ ![イメージ説明](08739ba32deb6cd6db085007791b2e1a.jpeg)
42
+
43
+
44
+
45
+ コードは以下のようになります。以下のコードの Employee クラスが List<T> の T になります。SQL Server からデータを取得して、それから List<Employee> オブジェクトを作成し、DataGridView に BindingSource 経由でバインドしています。foreach は書く必要はありません。
46
+
47
+
48
+
49
+ ```
50
+
51
+ using System;
52
+
53
+ using System.Collections.Generic;
54
+
55
+ using System.ComponentModel;
56
+
57
+ using System.Data;
58
+
59
+ using System.Drawing;
60
+
61
+ using System.Linq;
62
+
63
+ using System.Text;
64
+
65
+ using System.Threading.Tasks;
66
+
67
+ using System.Windows.Forms;
68
+
69
+ using System.Data.SqlClient;
70
+
71
+
72
+
73
+ namespace WindowsFormsApplication1
74
+
75
+ {
76
+
77
+ public partial class Form7 : Form
78
+
79
+ {
80
+
81
+ private DataGridView dataGridView1;
82
+
83
+ private BindingSource bindingSource1;
84
+
85
+
86
+
87
+ public Form7()
88
+
89
+ {
90
+
91
+ InitializeComponent();
92
+
93
+
94
+
95
+ this.dataGridView1 = new DataGridView();
96
+
97
+ this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
98
+
99
+ this.bindingSource1 = new BindingSource();
100
+
101
+ this.dataGridView1.DataSource = this.bindingSource1;
102
+
103
+ this.Controls.Add(this.dataGridView1);
104
+
105
+
106
+
107
+ this.bindingSource1.DataSource = GetEmployeeList();
108
+
109
+ }
110
+
111
+
112
+
113
+ private List<Employee> GetEmployeeList()
114
+
115
+ {
116
+
117
+ string connString = @"Data Source=(local)\sqlexpress;Initial Catalog=NORTHWIND;Integrated Security=True";
118
+
119
+ string query = "SELECT [EmployeeID],[LastName],[FirstName],[Address] FROM [Employees]";
120
+
121
+ List<Employee> list = new List<Employee>();
122
+
123
+
124
+
125
+ using (SqlConnection connection = new SqlConnection(connString))
126
+
127
+ {
128
+
129
+ using (SqlCommand command = new SqlCommand(query, connection))
130
+
131
+ {
132
+
133
+ connection.Open();
134
+
135
+ using (SqlDataReader reader = command.ExecuteReader())
136
+
137
+ {
138
+
139
+ while(reader.Read())
140
+
141
+ {
142
+
143
+ Employee emp = new Employee
144
+
145
+ {
146
+
147
+ Id = reader.GetInt32(0),
148
+
149
+ Name = reader.GetString(2) + " " + reader.GetString(1),
150
+
151
+ Address = reader.GetString(3)
152
+
153
+ };
154
+
155
+ list.Add(emp);
156
+
157
+ }
158
+
159
+ }
160
+
161
+ }
162
+
163
+ }
164
+
165
+
166
+
167
+ return list;
168
+
169
+ }
170
+
171
+ }
172
+
173
+
174
+
175
+ public class Employee
176
+
177
+ {
178
+
179
+ public int Id { get; set; }
180
+
181
+ public string Name { get; set; }
182
+
183
+ public string Address { get; set; }
184
+
185
+ }
186
+
187
+ }
188
+
189
+ ```
190
+
191
+
192
+
193
+ 実行結果は以下のようになります。
194
+
195
+
196
+
197
+ ![イメージ説明](612d526204799c78c1d25101c28c42a8.jpeg)
198
+
199
+
200
+
201
+ ところで、
202
+
203
+
204
+
205
+ > 「DataSet / DataTable + TableAdapter を作り、それを BindingSource 経由で DataGridView にバインドするのが定番の方法」とありましたが、チュートリアルを見ても理解することが難しかったので活用できませんでした、
206
+
207
+
208
+
209
+ とのことですが、一行もコードを書かずに済むので難しなんてことはあり得ません。そちらもやってみてください。やる気の問題かと思います。