Created
September 12, 2017 22:31
-
-
Save mak-oh-1977/032794a958f964f4cba7e21d16347005 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Windows.Forms; | |
namespace DataGridViewCombo | |
{ | |
public partial class Form1 : Form | |
{ | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
/// <summary> | |
/// コンボボックスデータの型定義 | |
/// </summary> | |
public class Fruits | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
} | |
/// <summary> | |
/// データグリッド1行分の型定義 | |
/// </summary> | |
public class Row | |
{ | |
public int FruitsId { get; set; } | |
} | |
/// <summary> | |
/// データグリッドにバインドされるデータ | |
/// </summary> | |
private BindingSource dataList = new BindingSource(); | |
private void Form1_Load(object sender, EventArgs e) | |
{ | |
// コンボボックスデータの用意 | |
BindingSource bc = new BindingSource(); | |
bc.Add(new Fruits() { Id = 0, Name = "Apple" }); | |
bc.Add(new Fruits() { Id = 1, Name = "Orange" }); | |
bc.Add(new Fruits() { Id = 2, Name = "Grape" }); | |
// グリッドの列用 | |
DataGridViewComboBoxColumn cbx = new DataGridViewComboBoxColumn(); | |
cbx.HeaderText = "fruits"; | |
cbx.DataSource = bc; | |
cbx.ValueMember = "Id"; //選択項目の値 | |
cbx.DisplayMember = "Name"; //コンボボックスの表示 | |
cbx.DataPropertyName = "FruitsId"; //データグリッドのデータとリンク | |
dgv.Columns.Add(cbx); | |
// データグリッドにバインドし値の設定取得 | |
dataList.DataSource = typeof(Row); | |
dgv.DataSource = dataList; | |
// 初期値として登録 | |
dataList.Add(new Row() { FruitsId = 1 }); | |
} | |
private void btn_Click(object sender, EventArgs e) | |
{ | |
foreach(Row o in dataList) | |
{ | |
Console.WriteLine(o.FruitsId); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment