有很多类似于我的问题,但我的问题与典型问题相反:当沿着详细记录集移动时,我无法正确显示主 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();
}
有人指出正确的解决方案吗?
谢谢