cscの作法 その328

# 概要
cscの作法、調べてみた。
練習問題、やってみた。

# 練習問題
comboboxを連携させよ。

# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/de71bd4f-6516-7a63-d3ff-1be0906503e3.png)


# サンプルコード

```
using System;
using System.Data;
using System.Windows.Forms;
using System.Drawing;

namespace App
{
	public partial class Form1: Form {
		private DataTable dataTable;
		ComboBox comboBox1;
		ComboBox comboBox2;
		ComboBox comboBox3;
		public Form1() {
			Text = "combo test";
			ClientSize = new Size(800, 300);
			comboBox1 = new ComboBox();
			comboBox1.Width = 100;
			comboBox1.Height = 25;
			comboBox1.Left = 50;
			comboBox1.Top = 50;
			this.Controls.Add(comboBox1);
			comboBox2 = new ComboBox();
			comboBox2.Width = 100;
			comboBox2.Height = 25;
			comboBox2.Left = 250;
			comboBox2.Top = 50;
			this.Controls.Add(comboBox2);
			comboBox3 = new ComboBox();
			comboBox3.Width = 100;
			comboBox3.Height = 25;
			comboBox3.Left = 450;
			comboBox3.Top = 50;
			this.Controls.Add(comboBox3);
			this.Load += new System.EventHandler(this.Form1_Load);
		}
		private void Form1_Load(object sender, EventArgs e) {
			dataTable = new DataTable("Japan");
			dataTable.Columns.Add("area", typeof(string));
			dataTable.Columns.Add("prefecture", typeof(string));
			dataTable.Columns.Add("city", typeof(string));
			dataTable.Rows.Add("関東", "東京", "世田谷");
			dataTable.Rows.Add("関東", "東京", "渋谷");
			dataTable.Rows.Add("関東", "東京", "中野");
			dataTable.Rows.Add("関東", "神奈川", "横浜");
			dataTable.Rows.Add("関東", "神奈川", "Kawasaki");
			dataTable.Rows.Add("関東", "埼玉", "Saitama");
			dataTable.Rows.Add("関東", "埼玉", "Fukaya");
			dataTable.Rows.Add("関東", "埼玉", "Koshigaya");
			dataTable.Rows.Add("関東", "埼玉", "Kawaguchi");
			dataTable.Rows.Add("関西", "福岡", "Fukuoka");
			dataTable.Rows.Add("関西", "長崎", "Nagasaki");
			dataTable.Rows.Add("関西", "鹿児島", "Kagoshima");
			DataTable resultDataTableForArea = dataTable.DefaultView.ToTable(true, new string[] {
				"area"
			});
			this.comboBox1.DisplayMember = "area";
			this.comboBox1.ValueMember = "area";
			this.comboBox1.DataSource = resultDataTableForArea;
			this.comboBox1.SelectedIndex = 0;
			this.InitializePrefectureComboBox();
			this.InitializeCityComboBox();
			comboBox1.SelectedValueChanged += comboBox1_TextChanged;
			comboBox1.TextChanged += comboBox1_TextChanged;
			comboBox2.SelectedValueChanged += comboBox2_TextChanged;
			comboBox2.TextChanged += comboBox2_TextChanged;
		}
		private void InitializePrefectureComboBox() {
			var resultDataTableForPrefecture = dataTable.AsEnumerable().Where(row => row.Field<string>("area") == this.comboBox1.SelectedValue.ToString()).AsDataView().ToTable(true, new string[] {
				"area",
				"prefecture"
			});
			this.comboBox2.DisplayMember = "prefecture";
			this.comboBox2.ValueMember = "prefecture";
			this.comboBox2.DataSource = resultDataTableForPrefecture;
			this.comboBox2.SelectedIndex = 0;
		}
		private void InitializeCityComboBox() {
			var resultDataTableForCity = dataTable.AsEnumerable().Where(row => row.Field<string>("area") == this.comboBox1.SelectedValue.ToString() && row.Field<string>("prefecture") == this.comboBox2.SelectedValue.ToString()).AsDataView().ToTable(true, new string[] {
				"area",
				"prefecture",
				"city"
			});
			this.comboBox3.DisplayMember = "city";
			this.comboBox3.ValueMember = "city";
			this.comboBox3.DataSource = resultDataTableForCity;
			this.comboBox3.SelectedIndex = 0;
		}
		private void comboBox1_TextChanged(object sender, EventArgs e) {
			this.InitializePrefectureComboBox();
			this.InitializeCityComboBox();
		}
		private void comboBox2_TextChanged(object sender, EventArgs e) {
			this.InitializeCityComboBox();
		}
		[STAThread]
		public static void Main() {
			Application.Run(new Form1());
		}
	}
}





```

以上。