回答編集履歴
1
追記
answer
CHANGED
@@ -4,4 +4,102 @@
|
|
4
4
|
|
5
5
|
で「List<T> 型を使いましょう」と提案させていただいて、その案に納得されたと思っていたんですが・・・
|
6
6
|
|
7
|
-
理解できていなかったのでしょうか? それとも List<T> を使う案ではやりたいことができないとか?
|
7
|
+
理解できていなかったのでしょうか? それとも List<T> を使う案ではやりたいことができないとか?
|
8
|
+
|
9
|
+
**【追伸】**
|
10
|
+
|
11
|
+
ホントは、質問者さんが先輩に聞くべき話だとは思いますが・・・
|
12
|
+
|
13
|
+
> foreachを省略してカンマ区切りでDataGridViewに表示する方法があれば教えていただきたいです。
|
14
|
+
|
15
|
+
そういう方法はなさそうです。少なくとも自分は知りません。
|
16
|
+
|
17
|
+
なので、質問者さんの前のスレッドで私が提案した List<T> 案を使って、foreach を使わないで済む方法を書いておきます。この案が質問者さんの目的に合わないなら、どこかどう合わないのか具体的に書いてください。
|
18
|
+
|
19
|
+
データベースには Microsoft が提供するサンプルデータベース Northwind の Employees テーブルを使ってみます。以下のような構造で、質問者さんの使っているものに近いのでは?
|
20
|
+
|
21
|
+

|
22
|
+
|
23
|
+
コードは以下のようになります。以下のコードの Employee クラスが List<T> の T になります。SQL Server からデータを取得して、それから List<Employee> オブジェクトを作成し、DataGridView に BindingSource 経由でバインドしています。foreach は書く必要はありません。
|
24
|
+
|
25
|
+
```
|
26
|
+
using System;
|
27
|
+
using System.Collections.Generic;
|
28
|
+
using System.ComponentModel;
|
29
|
+
using System.Data;
|
30
|
+
using System.Drawing;
|
31
|
+
using System.Linq;
|
32
|
+
using System.Text;
|
33
|
+
using System.Threading.Tasks;
|
34
|
+
using System.Windows.Forms;
|
35
|
+
using System.Data.SqlClient;
|
36
|
+
|
37
|
+
namespace WindowsFormsApplication1
|
38
|
+
{
|
39
|
+
public partial class Form7 : Form
|
40
|
+
{
|
41
|
+
private DataGridView dataGridView1;
|
42
|
+
private BindingSource bindingSource1;
|
43
|
+
|
44
|
+
public Form7()
|
45
|
+
{
|
46
|
+
InitializeComponent();
|
47
|
+
|
48
|
+
this.dataGridView1 = new DataGridView();
|
49
|
+
this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
|
50
|
+
this.bindingSource1 = new BindingSource();
|
51
|
+
this.dataGridView1.DataSource = this.bindingSource1;
|
52
|
+
this.Controls.Add(this.dataGridView1);
|
53
|
+
|
54
|
+
this.bindingSource1.DataSource = GetEmployeeList();
|
55
|
+
}
|
56
|
+
|
57
|
+
private List<Employee> GetEmployeeList()
|
58
|
+
{
|
59
|
+
string connString = @"Data Source=(local)\sqlexpress;Initial Catalog=NORTHWIND;Integrated Security=True";
|
60
|
+
string query = "SELECT [EmployeeID],[LastName],[FirstName],[Address] FROM [Employees]";
|
61
|
+
List<Employee> list = new List<Employee>();
|
62
|
+
|
63
|
+
using (SqlConnection connection = new SqlConnection(connString))
|
64
|
+
{
|
65
|
+
using (SqlCommand command = new SqlCommand(query, connection))
|
66
|
+
{
|
67
|
+
connection.Open();
|
68
|
+
using (SqlDataReader reader = command.ExecuteReader())
|
69
|
+
{
|
70
|
+
while(reader.Read())
|
71
|
+
{
|
72
|
+
Employee emp = new Employee
|
73
|
+
{
|
74
|
+
Id = reader.GetInt32(0),
|
75
|
+
Name = reader.GetString(2) + " " + reader.GetString(1),
|
76
|
+
Address = reader.GetString(3)
|
77
|
+
};
|
78
|
+
list.Add(emp);
|
79
|
+
}
|
80
|
+
}
|
81
|
+
}
|
82
|
+
}
|
83
|
+
|
84
|
+
return list;
|
85
|
+
}
|
86
|
+
}
|
87
|
+
|
88
|
+
public class Employee
|
89
|
+
{
|
90
|
+
public int Id { get; set; }
|
91
|
+
public string Name { get; set; }
|
92
|
+
public string Address { get; set; }
|
93
|
+
}
|
94
|
+
}
|
95
|
+
```
|
96
|
+
|
97
|
+
実行結果は以下のようになります。
|
98
|
+
|
99
|
+

|
100
|
+
|
101
|
+
ところで、
|
102
|
+
|
103
|
+
> 「DataSet / DataTable + TableAdapter を作り、それを BindingSource 経由で DataGridView にバインドするのが定番の方法」とありましたが、チュートリアルを見ても理解することが難しかったので活用できませんでした、
|
104
|
+
|
105
|
+
とのことですが、一行もコードを書かずに済むので難しなんてことはあり得ません。そちらもやってみてください。やる気の問題かと思います。
|