このコードの何が問題になっていますか?
myComboBox.Items.Clear();
myComboBox.Items.AddRange(new string[]{"one","two"});
myComboBox.SelectedValue = "one";
何も選択されていない状態で表示されます。
次のようにコンボボックスに入力すると:
myComboBox.Items.AddRange(new string[]{"one","two"});
選択した項目を設定/取得するには、ComboBox.SelectedItem
またはプロパティを使用する必要があります。ComboBox.SelectedIndex
myComboBox.SelectedItem = "one"; //or
myComboBox.SelectedIndex = 0;
ComboBox.SelectedValue
プロパティはから継承され 、次の場合にのみListControl
使用する必要があります。
- コントロールはにバインドされています
DataSource
- andプロパティが定義さ
ValueMember
れています。DisplayMember
いくつかの異なるオプション:
1)SelectedValue
に変更SelectedIndex
myComboBox.SelectedIndex = 0; //your first item
これは無視してください。これはasp.net用です
2)ListItem
手動で追加する
myComboBox.Items.Clear();
myComboBox.Items.Add(new ListItem() { Text = "one", Selected = true };
myComboBox.Items.Add(new ListItem() { Text = "two" };
一度に複数のアイテムが選択されていないことを確認してください。