1

辞書をデータソースとして使用するリストボックスがあります。

リストボックスのselectedvalueをint変数に解析したい場合、キャスト例外が発生します。

辞書〜

Dictionary<int, string> AssetDictionary = new Dictionary<int, string>();

リストボックス(lstAsset)データソース〜

lstAsset.DisplayMember = "Value";
//lstAssetType.ValueMember = "Key";   //This should be lstAsset as corrected in the next line
lstAsset.ValueMember = "Key";
lstAsset.DataSource = new BindingSource(AssetDictionary, null);

例外が発生する行〜

int ush = (int)lstAsset.SelectedValue; //Specified cast is not valid.

どこが間違っているのですか?

4

1 に答える 1

2

コントロールを修正するためにバリューメンバーを提供します。

lstAsset.ValueMember = "Key";

と使用

int ush = Convert.ToInt32(lstAsset.SelectedValue.ToString());
于 2013-03-24T16:15:16.233 に答える