2

String名前int用の と ID 番号用のを持つクラスがあります。

public class Item
{
    public int IDNumber { get; set; }
    public String Name  { get; set; }
}

List<Item>作成するために使用したものがありますBindingList<Item>。はBindingList<Item>、私がデータバインドDataSourceした です。ComboBox私はDisplay Member現在、名前として、Value Member現在 IDNumber として持っています。

を変更することはできますが、変更するNameと の値ComboBox.SelectedTextが "" になります。より明確にするために、名前は「Dave」と伝えます。ユーザーは「John」と入力します。を「ジョン」に変更したいのですSelectedTextが、代わりに「」になります。

私は使用してみましたINotifyPropertyChanged

public class Item : INotifyPropertyChanged
{
    private String name;

    public int IDNumber { get; set; }
    public String Name
    {
        get
        {
            return name;
        }
        set
        {
            this.name = value;
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs("Name"));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}

ComboBoxこれは、フィールドを使用する前に一度クリックした後にのみトリックを実行しますがSelectedText、変更が加えられたときに更新したいのです。

これを回避する方法を知っている人はいますか?

ありがとう。

4

2 に答える 2

2

バインディングソースを使用する

呼び出しリセットバインディング

private BindingSource bs;

private SetupBinding()
{
    List<Item> data = new List<Item>();


    //Get Data 



    bs = new bindingsource();
    bs.datasource = data;
    combobox.datasource = bs;
    comboBox.DisplayMember="Name";
    comboBox.ValueMember="IDNumber";
}

private ShowMyMessage()
{
    MessageBox.Show(this, message, caption, buttons,
            MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, 
            MessageBoxOptions.RightAlign);
 if (bs != null)
 {
   bs.resetbindings(false);
 }
}  

次に、コンボボックスに何かが選択されていることを確認し、selectedtextを試してください

于 2012-07-10T17:48:31.377 に答える
1

comboBox.SelectedValueを使用する

したがって、DisplayMemberとValueMemberを設定する必要があります

お気に入り

comboBox.DisplayMember="Name";
comboBox.ValueMember="IDNumber";

最後にデータソースを設定します

于 2012-07-10T15:23:39.580 に答える