DropDownListにDictionaryをバインドする方法を調べることがあったので、忘れないようにメモ。
1 | Dictionary< int , string > dic = |
2 | new Dictionary< int , string > { |
3 | { 1, "りんご" }, |
4 | { 2, "ごりら" }, |
5 | { 3, "ラッパ" } |
6 | }; |
7 | this .DropDownList1.DataSource = dic; |
8 | this .DropDownList1.DataTextField = "value" ; |
9 | this .DropDownList1.DataValueField = "key" ; |
10 | this .DropDownList1.DataBind(); |
データベースのテーブルをドロップダウンに表示させるときは、型付データセットを使ってやってるけど、テーブルに持つほどもないリストのときはリスト内容をDictionaryでメンバー変数にでも持っておけば、内容変わっても一箇所で管理できるな。
それにkeyの値から簡単にvalueの値も取れるし。
あっ。
ぼちぼちコードをきれいに表示するプラグインを導入せねば・・・。