0

有很多类似于我的问题,但我的问题与典型问题相反:当沿着详细记录集移动时,我无法正确显示主 Combobox 中的项目。我对 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();         
     }

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

谢谢

4

1 回答 1

0

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

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

我相信必须存在更好和更多的 ADO 功能 - 意识,但这无论如何都挽救了我的星期天。

于 2013-07-07T10:53:50.310 回答