无法从另一个comboBox更新comboBox

时间:2013-12-15 19:09:35

标签: c# combobox

private void tabControl1_Selected(object sender, TabControlEventArgs e)
    {
        if (e.TabPage.Name == tabPage2.Name)
        {
            table = Items.Get();
            comboBox1.DataSource = table;
            comboBox1.DisplayMember = "Item_ID";
        }
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataTable temp = new DataTable();
        string text = comboBox1.SelectedItem.ToString();
        temp = Color.Get(text);
        comboBox2.DataSource = temp;
        comboBox2.DisplayMember = "Color_Name";
        comboBox2.ValueMember = "Color_ID";
    }

我正在尝试填充comboBox1作为打开的tabpage,然后根据comboBox1的selectedText填充comboBox2。

comboBox_SelectedIndexChange在制表符更改时运行2次,但每次都返回null。

注意:我已经附加了事件处理程序,因为表单初始化为

public Form1()
    {
        InitializeComponent();
        tabControl1.Selected += new TabControlEventHandler(tabControl1_Selected);
        comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;

        table = new DataTable();
        s = new Stock();
    }

3 个答案:

答案 0 :(得分:0)

首先

中有一个错误
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    DataTable temp = new DataTable();
    string text = comboBox1.SelectedItem.ToString();
    temp = Color.Get(text);
    comboBox2.DataSource = temp;
    comboBox2.DisplayMember = "Color_Name";
    comboBox2.ValueMember = "Color_ID";
}

代码。您在行

中覆盖数据源的值
temp = Color.Get(text);

我认为应该是这样的:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    DataTable temp = new DataTable();
    string text = comboBox1.SelectedItem.ToString();
    selectedColor = Color.Get(text);
    comboBox2.DataSource = temp;
    comboBox2.DisplayMember = "Color_Name";
    comboBox2.ValueMember = "Color_ID";
    comboBox2.SelectedItem = selectedColor;
}

我不知道DataTable的内容,因此您可能需要设置comboBox2.SelectedItem的{​​{1}},SelectedItemSelectedText属性,而不是SelectedValue

答案 1 :(得分:0)

这里的可疑行:

string text = comboBox1.SelectedItem.ToString();

您将获得填充了“YourNamespace.DataTable”的文本变量。如果您的函数Color.Get(text)期望所选项的Item_ID作为参数,那么您应该将上面的代码行更改为:

string text = ((DataTable)comboBox1.SelectedItem).Item_ID;

我假设DataTable是一个具有Item_ID属性的对象。

答案 2 :(得分:0)

private void tabControl1_Selected(object sender, TabControlEventArgs e)
    {
        if (e.TabPage.Name == tabPage2.Name)
        {
            table = Items.Get();
            if (table.Rows.Count > 0)
            {
                //Update comboBox1 using table
                comboBox1.DataSource = table;
                comboBox1.DisplayMember = "Item_ID";

                //Using 1st row and 1st coloumn in function argument to get colors
                //Color.Get(string itemID) returns dataTable, which I used for comboBox2 DataSource
                comboBox2.DataSource = Color.Get(table.Rows[0].ItemArray[0].ToString());
                comboBox2.ValueMember = "Color_ID";
                comboBox2.DisplayMember = "Color_Name";
            }
        }
    }