如何刷新组合框winform c#

时间:2017-12-07 02:42:58

标签: c# oracle winforms visual-studio combobox

我使用带有两个组合框的c#创建winform

The form look like this

combobox1从数据库中选择公司

combobox2从数据库中选择项目取决于来自combobox1的公司

当表单加载时一切正常,但我想在更改combobox1中的公司时刷新combobox2

这是我的代码

using System;
using System.Data;
using System.Windows.Forms;
using Oracle.ManagedDataAccess.Client;

namespace WindowsFormsApp3
{
    public partial class Form_AC071503 : Form
    {
        public Form_AC071503()
        {
            InitializeComponent();
        }

        DataSet ds = new DataSet();

        OracleCommand cmd1;
        OracleDataAdapter da1;

        OracleCommand cmd2;
        OracleDataAdapter da2;

        private void Form_AC071503_Load(object sender, EventArgs e)
        {
            cmd1 = new OracleCommand("SELECT company,company_name from COMPANIES", Class_All.Connection);
            da1 = new OracleDataAdapter(cmd1);
            da1.Fill(ds, "COMPANIES");
            input_company.DataSource = ds.Tables["COMPANIES"];
            input_company.DisplayMember = "company_name";
            input_company.ValueMember = "company";

            cmd2 = new OracleCommand("SELECT project,project_name from PROJECTS where COMPANY = '" + input_company.SelectedValue.ToString() + "'", Class_All.Connection);
            da2 = new OracleDataAdapter(cmd2);
            da2.Fill(ds, "PROJECTS");
            input_project.DataSource = ds.Tables["PROJECTS"];
            input_project.DisplayMember = "project_name";
            input_project.ValueMember = "project";
        }

        private void Input_company_SelectedIndexChanged(object sender, EventArgs e)
        {
            /*i try to use the same code in Form_AC071503_Load like this

              cmd2 = new OracleCommand("SELECT project,project_name from PROJECTS where COMPANY = '" + input_company.SelectedValue.ToString() + "'", Class_All.Connection);
              da2 = new OracleDataAdapter(cmd2);
              da2.Fill(ds, "PROJECTS");
              input_project.DataSource = ds.Tables["PROJECTS"];
              input_project.DisplayMember = "project_name";
              input_project.ValueMember = "project";

              but not work because the list project not refresh but increased with new value*/ 
        }
    }
}

谢谢你的建议。

1 个答案:

答案 0 :(得分:0)

您需要在combobox1中添加ValueChanged事件。