2011-12-21 10 views
1

私は、データ(ディスプレイと値)をいくつかのデータソースからプルするフォーム上にコンボボックスコントロールを持っています。別の面では、私は1列のテーブルを持っています。私はアプリがlauching、コンボボックスがselectedvalueまたはselecteditemを上記の行の1列の値に設定するときにしたい。そして、ユーザーがコンボボックスを変更したとき、それは行への変更を持続します。 SelectedValueをこの列にバインドしようとしましたが、機能しません。コンボボックスは開始から最初の項目に設定されます。問題は何ですか?ComboBoxデータバインディング


EDIT

これが勝利フォームプロジェクトです。ここで バインディングコードです:私は以下の私のコメントで述べたように

this.comboBoxCountries = new System.Windows.Forms.ComboBox(); 
this.countriesBindingSource = new System.Windows.Forms.BindingSource(this.components); 

// 
// comboBoxCountries 
// 
this.comboBoxCountries.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.searchCriteriaBindingSource, "Postcode", true)); 
this.comboBoxCountries.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.searchCriteriaBindingSource, "CountryCode", true)); 
this.comboBoxCountries.DataSource = this.countriesBindingSource; 
this.comboBoxCountries.DisplayMember = "Name"; 
this.comboBoxCountries.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; 
this.comboBoxCountries.FormattingEnabled = true; 
this.comboBoxCountries.Location = new System.Drawing.Point(190, 19); 
this.comboBoxCountries.Name = "comboBoxCountries"; 
this.comboBoxCountries.Size = new System.Drawing.Size(156, 21); 
this.comboBoxCountries.TabIndex = 2; 
this.comboBoxCountries.ValueMember = "Code"; 
this.comboBoxCountries.SelectedValueChanged += new System.EventHandler(this.comboBoxCountries_SelectedValueChanged); 

// 
// countriesBindingSource 
// 
this.countriesBindingSource.DataMember = "Countries"; 
this.countriesBindingSource.DataSource = this.dbDataSetCountries; 
// 
// dbDataSetCountries 
// 
this.dbDataSetCountries.DataSetName = "dbDataSetCountries"; 
this.dbDataSetCountries.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema; 

// 
// searchCriteriaBindingSource 
// 
this.searchCriteriaBindingSource.AllowNew = false; 
this.searchCriteriaBindingSource.DataMember = "SearchCriteria"; 
this.searchCriteriaBindingSource.DataSource = this.dbDataSetSearchCriteria; 
this.searchCriteriaBindingSource.BindingComplete += new System.Windows.Forms.BindingCompleteEventHandler(this.searchCriteriaBindingSource_BindingComplete); 
// 
// dbDataSetSearchCriteria 
// 
this.dbDataSetSearchCriteria.DataSetName = "dbDataSetSearchCriteria"; 
this.dbDataSetSearchCriteria.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema; 

EDIT2

、私は同じ結合元とtextbox作業の別のDataMemberにバインドされ、別のtextboxを持っています良い。それは適切な価値を持って現れます。私がselectedvalueコンボボックスのバインディングのプロパティを設定した同じdatamemberのDataMemberを変更すると、良い結果が得られ、正しく動作します。

ありがとうございます!

+0

がある(場合で結合することを配置してみてくださいポストバック)機能 – prema

+0

このwinforms/wpf/asp.netはどの技術ですか?あなたが試したコードは? – Maheep

+0

データソース定義だけでなく、データバインディングコード(ItemsSourceとSelectedItem/Value)も投稿してください。 –

答えて

4

DisplayMemberValueMemberのコンボボックスのプロパティを見てください。 ComboBoxに、ドロップダウンに表示するデータソースのメンバーと、SelectedValueが要求されたときに与える値を指定する必要があります。

あなたの行が表示されていない間、あなたのComboBoxが静的リストにバインドされているようです。 ComboBoxとDataGridViewのDataSourceを設定するBindingSourceの使用を検討することもできます。このようにしてDGVが新しい行に移動すると、ComboBoxは新しい行の値で更新されます。ここで

は、MSDN上のコンボボックスへのリンクです:http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.aspx

+0

私の編集した質問を見てください。 – kseen

3

私はそれを見つけます。したがって、この問題を管理するために、あなたは、Visual StudioのデータバインドメニューからSelectedValueのデータバインディングを削除し、すべてのbindingsourcesを充填した後、いくつかの場所でこのデータバインディングを追加するために、適切なコードを配置する必要があります!

private void MainForm_Load_1(object sender, EventArgs e) 
{ 
    this.searchCriteriaTableAdapter1.Fill(this.dbDataSetCountries.SearchCriteria);  

    this.searchCriteriaTableAdapter.Fill(this.dbDataSetSearchCriteria.SearchCriteria);  

    comboBoxCountries.DataBindings.Add("SelectedValue", this.dbDataSetCountries.SearchCriteria, "CountryCode");     
} 
関連する問題