2016-04-18 5 views
2

DataGridViewをフォーム上の「行セレクタ」として使用し、多数のコントロールがbindingSourceにバインドされています。ComboBox.SelectedValueが期待どおりに機能しない

バインドされたコントロールの1つは、行のステータス選択を可能にするルックアップとして機能するComboBoxです。これは、DataTableからDBから取得されたデータを使用して作成されます。

このボックスの人口は問題ありません。

DGVから特定の行を選択すると、フォームコントロールは指定された行のデータを必要に応じて表示しますが、「statusComboBox」はあまりゲームをプレイしていません。

DGV内で以前に選択したステータスとは異なるステータスを持つ行を選択すると、それは必要に応じて機能しますが、以前に選択した行と同じ値の行を選択すると、 ValueMemberを示すDisplayMemberを示します。

上記のシナリオでは、行の選択によってバインドされたComboBoxからの表示応答のみが行われ、以前の選択には異なる「ステータスID」が設定されているようです。この行動を引き起こす原因は何ですか?

したがってフォーム負荷がこの

private void ProjectsForm_Load(object sender, EventArgs e) 
{ 
    InitBindingSource(); 

    //// bind Selector 
    //ASMod$ this needs to be 'true' unless you explicitly declare columns 
    ProjectsDataGridView.AutoGenerateColumns = false; 
    ProjectsDataGridView.DataSource = ProjectsBindingSource; 

    GetData(); 

    //Set GeneralStatusBox 
    Helpers.GeneralStatusInitLookup(statusComboBox, ProjectsBindingSource); 
} 

ProjectBindingSourceは、このように初期化されているように見える:A ProjectsAddDataBindings手順、およびコンボボックスのために含まDataBindings.Addは(の終わりに

private void InitBindingSource() 
{ 
    ProjectsBindingSource = new BindingSource(); 
    projectsBindingNavigator.BindingSource = ProjectsBindingSource; 
    ProjectsBindingSource.PositionChanged += new EventHandler(ProjectsBindingSource_PositionChanged); 
} 

実行ProjectsBindingSourceを追加したGetDataルーチン)。

ProjectsAddDataBindings(); 
{ 
    … 
    this.statusComboBox.DataBindings.Add("Text", ProjectsBindingSource, "GSID"); 
    … 
} 
GeneralStatusInitLookupそれは

public static void GeneralStatusInitLookup(System.Windows.Forms.ComboBox comboBox, BindingSource primaryBindingSource) 
{ 
    string statusFilter = ""; 
    statusFilter = Helpers.GetStatusGroupFilter(EndeavourForm.FilterId); 
    if (statusFilter != "") 
    { 
     statusFilter = " WHERE " + statusFilter; 
    } 
    //// string statusFilter = ""; //// temp 

    string sql = ""; 
    sql = "SELECT GSID, ShortName FROM GeneralStatus" + statusFilter + " ORDER BY Pos"; 
    GeneralStatusDataTable = Helpers.Db.GetDataTable(sql); 

    comboBox.DataSource = GeneralStatusDataTable; 
    comboBox.DisplayMember = "ShortName"; 
    comboBox.ValueMember = "GSID"; 

    comboBox.DataBindings.Add(new Binding("SelectedValue", primaryBindingSource.DataSource, "GSID")); 
} 

異なる形態の数に機能を提供し、DGV行変更がこの

private void ProjectsBindingSource_PositionChanged(object sender, EventArgs e) 
{ 
    try 
    { 
     // Update the database with the user's changes. 
     UpdateProjects(); 
     statusComboBox.SelectedValue = (int)CurrentDataRowView.Row["GSID"]; 
    } 
    catch (Exception) 
    { 
    } 
} 

private void UpdateProjects() 
{ 
    try 
    { 
     ProjectsDataAdapter.Update((DataTable)ProjectsBindingSource.DataSource); 

     DataHelper.CommitProposedChanges(projectsDataSet); 
     if (this.projectsDataSet.HasChanges() == true) 
     { 
      ProjectsBindingSource.EndEdit(); 
      ProjectsDataAdapter.Update(); 
     } 

     CurrentDataRowView = (DataRowView)ProjectsBindingSource.Current; 
    } 
    catch (InvalidOperationException) 
    { 
     throw; 
    } 
    catch (Exception) 
    { 
     throw; 
    } 
} 
ように処理される開始単にためのヘルパークラスで、ルックアップ要素を移入函ブロックの後

とにかく、私は多くのコードで読者を圧倒していないことを願っていますが、率直に言って、これはどこが間違っているのかはわかりません。だから、どんな助けでも大歓迎です。

答えて

0

これは最後の簡単な解決策でした。 GeneralStatusInitLookup()とProjectsAddDataBindings()ブロックの両方でDataBindings.Addが使用されました。ルックアップテーブルの場合、これは正常でしたが、メインテーブルへのバインディングを使用していました。後で、私は "Text"をpropertyNameパラメータとして使用しました。