List
からKeyValuePair<int, string>
のコンボボックスを作成しています。これまでは、私に数字のIDを返す間に、ユーザーに説明的な名前を提供するうえで非常にうまくいっています。
しかし、私が何をしようと、私は最初に選択した値を選択することができません。DataSourceとしてKeyValuePairのリストを持つComboBoxの最初に選択した値を選択
public StartUpForm()
{
InitializeComponent();
FlowLayoutPanel flowLayout = new FlowLayoutPanel(); //This is necessary to protect the table, which is for some reason collapsing...
flowLayout.FlowDirection = FlowDirection.TopDown;
flowLayout.AutoSize = true;
flowLayout.AutoSizeMode = AutoSizeMode.GrowAndShrink;
var comboBox = new ComboBox();
{
var choices = new List<KeyValuePair<int, string>>();
choices.Add(new KeyValuePair<int, string>(1, "hello"));
choices.Add(new KeyValuePair<int, string>(2, "world"));
comboBox.DataSource = choices;
comboBox.ValueMember = "Key";
comboBox.DisplayMember = "Value";
flowLayout.Controls.Add(comboBox);
}
Controls.Add(flowLayout);
//None of these work:
comboBox.SelectedValue = 2;
comboBox.SelectedValue = 2.ToString();
comboBox.SelectedValue = new KeyValuePair<int, string>(2, "world");
comboBox.SelectedValue = "world";
comboBox.SelectedItem = 2;
comboBox.SelectedItem = 2.ToString();
comboBox.SelectedItem = new KeyValuePair<int, string>(2, "world");
comboBox.SelectedItem = "world";
return;
}
結果は常に同じです:
私はデータソースList<KeyValuePair<int, string>>
として使用してComboBox
に最初に選択された値を選択することができますどのように?
素晴らしいです!ありがとう! 1つの質問:どのようにしてこれを知っていますか? – Antonio
@Antonio同じ問題がありました。 。 。何年も何年も前です。 – LarsTech