サンプルです。
コード量は少し多めに見えますが、ほとんどがデータ作成のためのものです。BindingSource もここで作っていますが、もちろんデザイナから作れますし、ComboBox のプロパティも本当はデザイナからの変更で大丈夫です。
まずはこれをコンパイル・実行して理解してから本番ソースに適用してください。書き換えるところは数行で済むと思うので、コピペではなく、理解してからの適用を強く推奨します。
デスクトップに KEN_ALL.CSV が必要です。KEN_ALL.CSV は読み仮名データの促音・拗音を小書きで表記するもの(zip形式) からダウンロードできます。
C#
1using System;
2using System.Collections.Generic;
3using System.Data;
4using System.IO;
5using System.Linq;
6using System.Text;
7using System.Text.RegularExpressions;
8using System.Windows.Forms;
9
10namespace WindowsForms1
11{
12 public partial class Form1 : Form
13 {
14 private DataSet データセット;
15 private BindingSource bindingSource1 = new BindingSource();
16 private BindingSource bindingSource2 = new BindingSource();
17
18 public Form1()
19 {
20 InitializeComponent();
21 }
22
23 private void Form1_Load(object sender, EventArgs e)
24 {
25 データセット = CreateDataSet();
26
27 bindingSource1.DataSource = データセット.Tables["データテーブル1"];
28 comboBox1.DataSource = bindingSource1;
29 comboBox1.DisplayMember = "Prefecture";
30 comboBox1.ValueMember = "Prefecture";
31 comboBox1.SelectedValueChanged += ComboBox1_SelectedValueChanged;
32
33 bindingSource2.DataSource = データセット.Tables["データテーブル2"];
34 comboBox2.DataSource = bindingSource2;
35 comboBox2.DisplayMember = "City";
36 }
37
38 private void ComboBox1_SelectedValueChanged(object sender, EventArgs e)
39 {
40 bindingSource2.Filter = string.Format(@"Prefecture like '{0}'", comboBox1.SelectedValue);
41 }
42
43 private IEnumerable<string> SplitCsv(string line)
44 {
45 var regex = new Regex(@"""(?<value>[^""]*)""|(?<value>[^,]*)");
46 for (int i = 0; i < line.Length;)
47 {
48 var match = regex.Match(line, i);
49 if (!match.Success) throw new Exception("不正な CSV");
50 yield return match.Groups["value"].Value;
51 i += match.Length + 1;
52 }
53 }
54
55 private DataSet CreateDataSet()
56 {
57 var desktopDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
58 var fileName = Path.Combine(desktopDir, "KEN_ALL.CSV");
59 var encoding = Encoding.GetEncoding("Shift_JIS");
60 DataSet result = new DataSet();
61
62 var table1 = result.Tables.Add("データテーブル1");
63 table1.Columns.Add("ID", typeof(int)).AutoIncrement = true;
64 table1.Columns.Add("Prefecture", typeof(string));
65
66 var table2 = result.Tables.Add("データテーブル2");
67 table2.Columns.Add("ID", typeof(int)).AutoIncrement = true;
68 table2.Columns.Add("Prefecture", typeof(string));
69 table2.Columns.Add("City", typeof(string));
70
71 var items = File
72 .ReadLines(fileName, encoding)
73 .Select(a => SplitCsv(a).ToArray())
74 .Select(a => new { Prefecture = a[6], City = a[7] })
75 .Distinct()
76 .ToList();
77 foreach (var item in items.Select(a => a.Prefecture).Distinct())
78 {
79 result.Tables["データテーブル1"].Rows.Add(null, item);
80 }
81 foreach (var item in items)
82 {
83 result.Tables["データテーブル2"].Rows.Add(null, item.Prefecture, item.City);
84 }
85
86 return result;
87 }
88 }
89}