Windows Forms(.NET 8)で提供されている ComboBox に、DataTable をバインドする方法を紹介します。実行可能サンプルも公開しますので、ダウンロードしていただき、すぐに動作を確認することができます。
目次
フォーム上に ComboBox を配置する
フォームデザイナを利用し、ComboBox と Button コントロールを配置しておきます。

DataTable を用意する
続いて、ComboBox にバインドする DataTable を用意します。コードの詳細は、コメントを参照ください。
private DataTable getData()
{
//DataTable オブジェクトの作成
DataTable dt = new DataTable();
//主キーオブジェクトの作成
DataColumn[] primaryColumn = new DataColumn[1];
//列の定義
primaryColumn[0] = dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Name", typeof(string));
//主キーの設定
dt.PrimaryKey = primaryColumn;
//ダミーデータの作成
dt.Rows.Add(1, "りんご");
dt.Rows.Add(2, "ぶどう");
dt.Rows.Add(3, "いちご");
return dt;
}
ComboBox に DataTable をバインドする
フォームのロードタイミングで ComboBox に DataTable をバインドし、ComboBox 各項目の値と DataTable 列のマッピング、ComboBox 各項目の表示テキストと DataTable 列のマッピングをそれぞれ行います。
private void Form1_Load(object sender, EventArgs e)
{
this.comboBox1.DataSource = getData(); //ComboBox に DataTable をバインド
this.comboBox1.ValueMember = "Id"; //ComboBox の値プロパティ
this.comboBox1.DisplayMember = "Name"; //ComboBox の表示プロパティ
}
ComboBox の選択項目の値を取得する
アプリケーション実行時の、ComboBox の選択項目の値や表示テキストを取得する実装を行います。ボタンクリック時のイベントに、下記を実装します。
private void button1_Click(object sender, EventArgs e)
{
//ComboBox の選択項目の取得
var item = this.comboBox1.SelectedItem;
//選択項目の Id 列の値
Debug.WriteLine((item as DataRowView)?.Row.ItemArray[0]);
//選択項目の Name 列の値(表示値)
Debug.WriteLine((item as DataRowView)?.Row.ItemArray[1]);
}
実行結果
ボタンクリック時に、SelectedItem に含まれる行の項目を Debug.WriteLite で出力ウィンドウに表示できていることがわかります。

サンプルアプリケーション
サンプルアプリケーションは、下記からダウンロードすることができます。