回答編集履歴
2
追記
answer
CHANGED
@@ -7,4 +7,92 @@
|
|
7
7
|
DataGridView に ComboBox を表示
|
8
8
|
[http://surferonwww.info/BlogEngine/post/2014/01/23/how-to-show-combobox-column-in-datagridview.aspx](http://surferonwww.info/BlogEngine/post/2014/01/23/how-to-show-combobox-column-in-datagridview.aspx)
|
9
9
|
|
10
|
-
上の記事は DataGridView の DataGridViewComboBoxColumn が対象ですが、普通の ComboBox でも同様にその DataSource プロパティを DataTable に、DisplayMember, ValueMember プロパティを DataTable の列名に設定することで可能だと思います。記事のコードを参考にしてください。
|
10
|
+
上の記事は DataGridView の DataGridViewComboBoxColumn が対象ですが、普通の ComboBox でも同様にその DataSource プロパティを DataTable に、DisplayMember, ValueMember プロパティを DataTable の列名に設定することで可能だと思います。記事のコードを参考にしてください。
|
11
|
+
|
12
|
+
**【追記】**
|
13
|
+
|
14
|
+
下のコメント欄で、
|
15
|
+
|
16
|
+
> 「連結した結果」を ComboBox の DispalyMember に設定して表示するサンプルを回答欄に追記しておきます。
|
17
|
+
|
18
|
+
と書きましたが、それを以下に追記しておきます。
|
19
|
+
|
20
|
+
先にも書きましたが、以下の記事のステップ 17 ~ の応用です。
|
21
|
+
|
22
|
+
DataGridView に ComboBox を表示
|
23
|
+
[http://surferonwww.info/BlogEngine/post/2014/01/23/how-to-show-combobox-column-in-datagridview.aspx](http://surferonwww.info/BlogEngine/post/2014/01/23/how-to-show-combobox-column-in-datagridview.aspx)
|
24
|
+
|
25
|
+
上の記事で使っている Northwind サンプルデータベースの Suppliers テーブルは以下のようになっています。
|
26
|
+
|
27
|
+

|
28
|
+
|
29
|
+
この SupplierID, CompanyName, ContactName フィールドを空間文字をはさんで連結して Name という名前で取得し、SupplierID と Name で DataTable を生成し、ComboBox の DataSource に設定します。そして ValueMember を "SupplierID" に、DisplayMember を "Name" に設定すれば完了です。
|
30
|
+
|
31
|
+
コードは以下の通り。
|
32
|
+
|
33
|
+
```
|
34
|
+
using System;
|
35
|
+
using System.Collections.Generic;
|
36
|
+
using System.ComponentModel;
|
37
|
+
using System.Data;
|
38
|
+
using System.Drawing;
|
39
|
+
using System.Linq;
|
40
|
+
using System.Text;
|
41
|
+
using System.Threading.Tasks;
|
42
|
+
using System.Windows.Forms;
|
43
|
+
using System.Data.SqlClient;
|
44
|
+
|
45
|
+
namespace WinFormsApp1
|
46
|
+
{
|
47
|
+
public partial class Form5 : Form
|
48
|
+
{
|
49
|
+
public Form5()
|
50
|
+
{
|
51
|
+
InitializeComponent();
|
52
|
+
|
53
|
+
this.comboBox1.DataSource = CreateSuppliersTable();
|
54
|
+
this.comboBox1.ValueMember = "SupplierID";
|
55
|
+
this.comboBox1.DisplayMember = "Name";
|
56
|
+
}
|
57
|
+
|
58
|
+
// Suppliers テーブルから DataTable を作るためのヘルパーメソッド
|
59
|
+
private DataTable CreateSuppliersTable()
|
60
|
+
{
|
61
|
+
DataTable table = new DataTable("Suppliers");
|
62
|
+
table.Columns.Add(new DataColumn("SupplierID", typeof(int)));
|
63
|
+
table.Columns.Add(new DataColumn("Name", typeof(string)));
|
64
|
+
|
65
|
+
string connString = WinFormsApp1.Properties.Settings.Default.NORTHWNDConnectionString;
|
66
|
+
string query = "SELECT [SupplierID], " +
|
67
|
+
"CONVERT(nvarchar,[SupplierID]) + ' ' + [CompanyName] + ' ' + [ContactName] AS Name " +
|
68
|
+
"FROM[NORTHWIND].[dbo].[Suppliers]";
|
69
|
+
|
70
|
+
using (SqlConnection conn = new SqlConnection(connString))
|
71
|
+
{
|
72
|
+
conn.Open();
|
73
|
+
using (SqlCommand cmd = new SqlCommand(query, conn))
|
74
|
+
{
|
75
|
+
using (SqlDataReader reader = cmd.ExecuteReader())
|
76
|
+
{
|
77
|
+
if (reader != null)
|
78
|
+
{
|
79
|
+
while (reader.Read())
|
80
|
+
{
|
81
|
+
DataRow row = table.NewRow();
|
82
|
+
row["SupplierID"] = reader.GetInt32(0);
|
83
|
+
row["Name"] = reader.GetString(1);
|
84
|
+
table.Rows.Add(row);
|
85
|
+
}
|
86
|
+
}
|
87
|
+
}
|
88
|
+
}
|
89
|
+
}
|
90
|
+
return table;
|
91
|
+
}
|
92
|
+
}
|
93
|
+
}
|
94
|
+
```
|
95
|
+
|
96
|
+
実行結果は以下の通り。
|
97
|
+
|
98
|
+

|
1
追記
answer
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
ログインとかの話はちょっと置いといて・・・
|
2
|
+
|
3
|
+
> DBのデータをComboBoxへの格納の仕方がわかりません。
|
4
|
+
|
1
5
|
以下の記事のステップ 17 ~ の応用でできると思います。
|
2
6
|
|
3
7
|
DataGridView に ComboBox を表示
|