我有一个使用字典作为数据源的列表框。
当我想将列表框的selectedvalue解析为int变量时,它会给我一个强制转换异常。
字典〜
Dictionary<int, string> AssetDictionary = new Dictionary<int, string>();
listbox(lstAsset)datasource~
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.
我在哪里做错了?
答案 0 :(得分:2)
提供价值会员以纠正控制。
lstAsset.ValueMember = "Key";
并使用
int ush = Convert.ToInt32(lstAsset.SelectedValue.ToString());