在主组合框中显示正确的项,该组合框与主/详细信息关系中的BindingNavigator耦合

本文关键字:组合 耦合 BindingNavigator 信息关系 显示 | 更新日期: 2023-09-27 18:21:03

有很多问题与我的问题类似,但我的问题与典型问题相反:当沿着详细记录集移动时,我无法正确显示主组合框中的项目。我对Ado.Net还很陌生,所以,请期待一些混乱:我对Ado.Net数据对象模型的了解仍然很差;不管怎样,这是我的代码:

     private DataSet MainDataSet           = new DataSet("MainDataSet");     
     BindingSource DetailBindingDataSource = new BindingSource();
     BindingSource MasterBindingDataSource = new BindingSource();
     SqlCeDataAdapter MasterDataAdapter;
     SqlCeDataAdapter DetailDataAdapter;
     MainDataSet.EnforceConstraints = true;
     MasterDataAdapter = new SqlCeDataAdapter(GET_MASTERS_SQL_COMMAND, CONNECTION_STRING);
     DetailDataAdapter = new SqlCeDataAdapter(GET_DETAILS_SQL_COMMAND, CONNECTION_STRING);
     GetData(MainDataSet, ImagesDataAdapter, "masters");       // just a wrapper
     GetData(MainDataSet, DefectsDataAdapter, "details");     // just a wrapper
     DataTable DetailTable = MainDataSet.Tables["details"];
     DataTable MasterTable = MainDataSet.Tables["masters"];           
     DetailBindingDataSource.DataSource = DetailTable;
     MasterBindingDataSource.DataSource = MasterTable;
// establishing relationships between Master / Detail data 
// to keep in sync related comboboxes with BindingNavigator
     DataRelation DetailHasMaster = new DataRelation("DetailHasMaster", MainDataSet.Tables["masters"].Columns["master"], MainDataSet.Tables["details"].Columns["details"]);
     MainDataSet.Relations.Add(DetailHasMaster);
     BindingNavigator SearchNavigator      = new BindingNavigator(true);
     SearchNavigator.BindingSource         = DetailBindingDataSource;    
     // Just as example, binding some fields: this is working, data change when moving on with BindingNavigator
     DataTextBox.DataBindings.Add(new Binding("Text", DetailBindingDataSource, "creationDate"));
     ItemTextBox.DataBindings.Add(new Binding("Text", DetailBindingDataSource, "partnumber"));
     SerialNumTextBox.DataBindings.Add(new Binding("Text", DetailBindingDataSource, "SerialNumber"));
     NoteTextBox.DataBindings.Add(new Binding("Text", DetailBindingDataSource, "note"));
/*
 * Assign data origin to the binding sources; 
 * this is NOT working, the master combobox do not changes when moving 
 * on with BindingNavigator
 */    
     MasterComboBox.DataSource = MasterBindingDataSource;
     MasterComboBox.DisplayMember = "name";
     MasterComboBox.ValueMember = "master";  
     private void GetData(DataSet CurrentDataSet, 
                          SqlCeDataAdapter CurrentDataAdapter, 
                          String TableName)
     {
          CurrentDataAdapter.FillSchema(CurrentDataSet, SchemaType.Source, TableName);
          CurrentDataAdapter.Fill(CurrentDataSet, TableName);
          SqlCeCommandBuilder GenericCommandBuilder = new SqlCeCommandBuilder(CurrentDataAdapter);
          CurrentDataAdapter.UpdateCommand = GenericCommandBuilder.GetUpdateCommand();
          CurrentDataAdapter.InsertCommand = GenericCommandBuilder.GetInsertCommand();         
     }

有人指出了正确的解决方案吗?

感谢

在主组合框中显示正确的项,该组合框与主/详细信息关系中的BindingNavigator耦合

不确定这是否是一个合适的解决方案:我的想法是,可以通过所涉及对象的某些属性或方法自动更新组合框。我实现的快速而肮脏的解决方案是:

  1. 订阅BindingSource.CurrentChanged事件,以获得有关步进数据的通知
  2. 检查:if(BindingSource.Count==0)
  3. 使用当前行中的值手动更新ComboBox.SelectedValue

我相信必须存在一个更好、更能感知ADO功能的系统,但这无论如何都挽救了我的周日。