DataGridViewのColumnTypeにDataGridViewComboBoxColumnを指定し、そのコンボボックスが選択している値を取得するサンプル

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace WindowsFormsApplication1
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.             // 1行追加
  18.             dataGridView1.Rows.Add();
  19.         }
  20.         private void button1_Click(object sender, EventArgs e)
  21.         {
  22.             // コンボボックスが未選択の場合
  23.             if (dataGridView1.Rows[0].Cells[0].Value == null)
  24.             {
  25.                 textBox1.Text = "null";
  26.             }
  27.             else
  28.             {
  29.                 textBox1.Text = dataGridView1.Rows[0].Cells[0].Value.ToString();
  30.             }
  31.         }
  32.     }
  33. }

image

実行ファイルダウンロード

戻る