Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 多组合框错误C-我得到的三个值都相同_C#_C# 4.0_Combobox - Fatal编程技术网

C# 多组合框错误C-我得到的三个值都相同

C# 多组合框错误C-我得到的三个值都相同,c#,c#-4.0,combobox,C#,C# 4.0,Combobox,我有三个combobox,它们使用我在项目中创建的相同数据库 当我在一个组合框中选择一个选项时,它会用相同的内容填充其他三个组合框。我可以得到它,这样我就可以为每个组合框选择不同的选项 // fieldBox1 // this.fieldBox1.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.fieldsTableBindingSource1, "F

我有三个combobox,它们使用我在项目中创建的相同数据库

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

// 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;

任何帮助都将是一个很大的帮助。

我没有您的周围代码,也无法测试它,但我认为这更符合您的需要。您需要定义三个单独的字段来保存组合框值。我把它们称为组合值1、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;

您正在将这三个绑定到同一个源。是。我需要创建3个数据库吗?它们在下拉菜单中都有相同的列表。如果我今天有时间,我会写一个更长的答案。源的绑定应该相同,但值是一个单独的绑定,并且应该绑定到三个不同的对象,因为当前值对于所有三个对象都不相同。