Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.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#_Winforms_Data Binding_Combobox_Listbox - Fatal编程技术网

C# 为什么我的数据绑定组合框中会出现过时数据?

C# 为什么我的数据绑定组合框中会出现过时数据?,c#,winforms,data-binding,combobox,listbox,C#,Winforms,Data Binding,Combobox,Listbox,我有一个表单,有两个数据绑定列表框和两个数据绑定组合框。我正在使用类型化数据集。这些控件被绑定到一对表,其中包含以下模式和来自该表的数据。一个列表框和一个组合框绑定到bar表;另一个列表框和组合框绑定到foo表 当为foo列表框触发SelectedIndexChanged事件时,我会在条形列表框和组合框中获取所选文本的当前值 但是,当我使用foo comboBox并尝试访问foo comboBox\u SelectedIndex Changed事件中的barComboBox.SelectedTe

我有一个表单,有两个数据绑定列表框和两个数据绑定组合框。我正在使用类型化数据集。这些控件被绑定到一对表,其中包含以下模式和来自该表的数据。一个列表框和一个组合框绑定到bar表;另一个列表框和组合框绑定到foo表

当为foo列表框触发SelectedIndexChanged事件时,我会在条形列表框和组合框中获取所选文本的当前值

但是,当我使用foo comboBox并尝试访问foo comboBox\u SelectedIndex Changed事件中的barComboBox.SelectedText时,我从SelectedText中获得以前选择的值,而不是新值。BarListBox.Selected为我提供了当前值

请注意,我使用傻瓜对话框进行选择,两个事件处理程序都按预期运行

有人能解释一下这里发生了什么,以及如何解决这个问题吗

表格截图和样本数据:

数据集设计器:

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);
        }
    }

我没有发现你解释的任何行为

请检查此代码,请纠正我复制的代码是错误的

我使用了组合框项和列表框项作为数据源从一些webServiceMethod添加到FormLoad中

在Form1.Designer.cs中

*在Form1.cs中*


我已经根据DataSource form WebService方法更改了数据,但在您检查beahaviour时,我仍然没有发现任何问题。

如果我的方法错误,请纠正我。所以我可以用新的答案更新你。我应该在我的问题中说得更清楚,但这段代码不使用数据绑定,它只是设置项目值。过几天我会提供一个更好的例子。@Aksay,也就是说,您的代码不使用数据绑定,而我的代码使用数据绑定,这似乎是奇怪之处的一部分。我已经更新了代码,正如您所说,我已经将项目作为DatSource从WebService方法绑定到ComboBox和ListBox
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);
  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();
    }