2013-02-12 47 views
1

我有一个2个数据绑定列表框和两个数据绑定组合框。我正在使用输入数据集。这些控件绑定到一对表,其中包含以下来自此schema的模式和数据。一个listbox和一个comboBox绑定到bar表;另一个listboxcomboBox绑定到foo表。为什么我的数据绑定组合框中出现陈旧数据?

当SelectedIndexChanged事件触发foo listBox时,我得到了栏listBoxcomboBox中选定文本的当前值。

然而,当我使用FOO comboBox和尝试访问barComboBox.SelectedTextFooComboBox_SelectedIndexChanged事件中,我得到SelectedText而不是新值之前选择的值内。 BarListBox.Selected给我的当前价值。

请注意,我使用FooListBox进行选择,两个事件处理函数都按预期工作。

任何人都可以解释这里发生了什么,以及如何解决这个问题?

表格截图W /样本数据:

enter image description here

DataSet设计器:

enter image description here

的Form1.cs的代码:

//Standard using statements and namespace info 

    public partial class Form1 : Form 
    { 
     //Loading DataSets and initializing here 

     private void FooListBox_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      Console.WriteLine("The value in the bar ListBox is {0}", barListBox.Text); 
     } 

     private void FooComboBox_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      Console.WriteLine("The value in the bar comboBox is {0}", barComboBox.Text); 
     } 
    } 

回答

0

我没有按照你的解释找到任何行为。

请认真核对这段代码,请做正确的我转载的代码是错误的。

我用组合框项目和列表框项FormLoad添加为数据源,从一些webServiceMethod

在Form1.Designer.cs

this.comboBox1 = new System.Windows.Forms.ComboBox(); 
      this.listBox1 = new System.Windows.Forms.ListBox(); 
      this.SuspendLayout(); 
      // 
      // comboBox1 
      // 
      this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; 
      this.comboBox1.FormattingEnabled = true; 
      this.comboBox1.Location = new System.Drawing.Point(32, 55); 
      this.comboBox1.Name = "comboBox1"; 
      this.comboBox1.Size = new System.Drawing.Size(188, 21); 
      this.comboBox1.TabIndex = 0; 
      this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged); 
      // 
      // listBox1 
      // 
      this.listBox1.FormattingEnabled = true; 
      this.listBox1.Location = new System.Drawing.Point(261, 55); 
      this.listBox1.Name = "listBox1"; 
      this.listBox1.Size = new System.Drawing.Size(255, 95); 
      this.listBox1.TabIndex = 1; 
      this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged); 

* 在Form1.cs*

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     MessageBox.Show("The value in the bar comboBox is "+ comboBox1.Text); 
    } 

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     MessageBox.Show("The value in the bar comboBox is "+ listBox1.Text); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     WebServiceRef.CM_ServiceSoapClient soapClient = new WebServiceRef.CM_ServiceSoapClient(); 

     comboBox1.DataSource = soapClient.GetAllCategories(); 

     listBox1.DataSource = soapClient.GetAllCategories(); 
    } 

我已经根据DataSource表单WebService方法更改了数据,但是我仍然没有发现任何问题,因为您已经完成了你的beahaviour。

+0

请纠正我如果我的做法是错误的。这样我可以用新的答案更新你。 – 2013-02-13 05:50:11

+0

我应该在我的问题中说得更清楚一点,但是这段代码不使用数据绑定,它只是设置项目值。我会在几天内提供一个更好的例子。 – yumaikas 2013-02-13 13:47:36

+0

@Aksay也就是说,你的代码不使用数据绑定和我的代码,这似乎是陌生的一部分。 – yumaikas 2013-02-13 18:15:09