0

我有三个组合框,使用我在项目中创建的相同数据库。

当我在一个组合框中选择一个选项时,它会用相同的东西填充其他三个。我可以得到它,这样我就可以为每个组合框选择不同的选择。

// fieldBox1
            // 
            this.fieldBox1.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.fieldsTableBindingSource1, "FieldName", true));
            this.fieldBox1.DataSource = this.fieldsTableBindingSource1;
            this.fieldBox1.DisplayMember = "FieldName";
            this.fieldBox1.FormattingEnabled = true;
            this.fieldBox1.Location = new System.Drawing.Point(498, 89);
            this.fieldBox1.Name = "fieldBox1";
            this.fieldBox1.Size = new System.Drawing.Size(121, 21);
            this.fieldBox1.TabIndex = 5;
            this.fieldBox1.ValueMember = "FieldName";
            this.fieldBox1.SelectedIndexChanged += new System.EventHandler(this.fieldBox1_SelectedIndexChanged);
            // 
            // fieldsDBDataSet1
            // 
            this.fieldsDBDataSet1.DataSetName = "FieldsDBDataSet1";
            this.fieldsDBDataSet1.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
            // 
            // fieldsTableBindingSource1
            // 
            this.fieldsTableBindingSource1.DataMember = "FieldsTable";
            this.fieldsTableBindingSource1.DataSource = this.fieldsDBDataSet1;
            // 
            // fieldsTableBindingSource
            // 
            this.fieldsTableBindingSource.AllowNew = false;
            this.fieldsTableBindingSource.DataMember = "FieldsTable";
            this.fieldsTableBindingSource.DataSource = this.fieldsDBDataSet1;
            // 
            // fieldBox2
            // 
            this.fieldBox2.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.fieldsTableBindingSource, "FieldName", true));
            this.fieldBox2.DataSource = this.fieldsTableBindingSource;
            this.fieldBox2.DisplayMember = "FieldName";
            this.fieldBox2.FormattingEnabled = true;
            this.fieldBox2.Location = new System.Drawing.Point(498, 116);
            this.fieldBox2.Name = "fieldBox2";
            this.fieldBox2.Size = new System.Drawing.Size(121, 21);
            this.fieldBox2.TabIndex = 6;
            this.fieldBox2.ValueMember = "FieldName";
            // 
            // fieldsTableBindingSource2
            // 
            this.fieldsTableBindingSource2.DataMember = "FieldsTable";
            this.fieldsTableBindingSource2.DataSource = this.fieldsDBDataSet1;
            // 
            // fieldBox3
            // 
            this.fieldBox3.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.fieldsDBDataSet1, "FieldsTable.FieldName", true));
            this.fieldBox3.DataSource = this.fieldsTableBindingSource3;
            this.fieldBox3.DisplayMember = "FieldName";
            this.fieldBox3.FormattingEnabled = true;
            this.fieldBox3.Location = new System.Drawing.Point(498, 140);
            this.fieldBox3.Name = "fieldBox3";
            this.fieldBox3.Size = new System.Drawing.Size(121, 21);
            this.fieldBox3.TabIndex = 7;
            this.fieldBox3.ValueMember = "FieldName";
            // 
            // fieldsTableBindingSource3
            // 
            this.fieldsTableBindingSource3.DataMember = "FieldsTable";
            this.fieldsTableBindingSource3.DataSource = this.fieldsDBDataSet1;

任何帮助都将是重大帮助。

4

1 回答 1

0

我没有您周围的代码,无法对此进行测试,但我认为这更符合您的要求。您需要定义三个单独的字段来保存 ComboBox 值。我将它们称为 ComboValue1、2 和 3。这三个的下拉源是相同的。

// Set up binding sources

// fieldsDBDataSet1
this.fieldsDBDataSet1.DataSetName = "FieldsDBDataSet1";
this.fieldsDBDataSet1.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;

// fieldsTableBindingSource - this is my data source for the ComboBox
this.fieldsTableBindingSource.AllowNew = false;
this.fieldsTableBindingSource.DataMember = "FieldsTable";
this.fieldsTableBindingSource.DataSource = this.fieldsDBDataSet1;

// Set up the ComboBoxes.  Here we make new bindings for the value:
// public Binding(string propertyName, Object dataSource, string dataMember, boolean formattingEnabled);
//   This constructs a binding that SETS propertyName to a value.
//   The value comes from dataMember, a property or member of the object dataSource.
// We also set the matching dataMember as our ValueMember, which must be different for all three boxes.
// To set the dropdown source we use the DisplayMember, which should be the same for all three boxes.

// fieldBox1
this.fieldBox1.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.fieldsTableBindingSource, "ComboValue1", true));
this.fieldBox1.ValueMember = "ComboValue1";
this.fieldBox1.DataSource = this.fieldsTableBindingSource;
this.fieldBox1.DisplayMember = "FieldName";
this.fieldBox1.FormattingEnabled = true;
this.fieldBox1.Location = new System.Drawing.Point(498, 89);
this.fieldBox1.Name = "fieldBox1";
this.fieldBox1.Size = new System.Drawing.Size(121, 21);
this.fieldBox1.TabIndex = 5;
this.fieldBox1.SelectedIndexChanged += new System.EventHandler(this.fieldBox1_SelectedIndexChanged);

// fieldBox2
this.fieldBox2.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.fieldsTableBindingSource, "ComboValue2", true));
this.fieldBox2.ValueMember = "ComboValue2";
this.fieldBox2.DataSource = this.fieldsTableBindingSource;
this.fieldBox2.DisplayMember = "FieldName";
this.fieldBox2.FormattingEnabled = true;
this.fieldBox2.Location = new System.Drawing.Point(498, 116);
this.fieldBox2.Name = "fieldBox2";
this.fieldBox2.Size = new System.Drawing.Size(121, 21);
this.fieldBox2.TabIndex = 6;

// fieldBox3
this.fieldBox3.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.fieldsTableBindingSource, "ComboValue3", true));
this.fieldBox3.ValueMember = "ComboValue3";
this.fieldBox3.DataSource = this.fieldsTableBindingSource;
this.fieldBox3.DisplayMember = "FieldName";
this.fieldBox3.FormattingEnabled = true;
this.fieldBox3.Location = new System.Drawing.Point(498, 140);
this.fieldBox3.Name = "fieldBox3";
this.fieldBox3.Size = new System.Drawing.Size(121, 21);
this.fieldBox3.TabIndex = 7;
于 2012-04-12T19:06:20.157 回答