如何根据第一个组合框中的选择过滤第二个组合框?

时间:2019-05-19 11:21:20

标签: c# winforms combobox oledb

当我单击第一个组合框(cmbHouse)中的项目时,我无法过滤第二个组合框(cmbRegion)。

我像这样填充2个组合框:

        //cmbHouse
        string cmbHouse_query = "SELECT * FROM House";
        OleDbDataAdapter dahouse = new OleDbDataAdapter(cmbHouse_query, connection);
        DataTable tablehouse = new DataTable();
        dahouse.Fill(tablehouse);
        cmbHouse.DataSource = tablehouse;
        cmbHouse.DisplayMember = "House_names"; 
        cmbHouse.ValueMember = "House.num_region"; //this is foreign key which equals to Region table's primary key
        cmbHouse.SelectedIndex = -1;

        //cmbRegion
        string cmbRegion_query = "SELECT * FROM Region";
        OleDbDataAdapter daregion = new OleDbDataAdapter(cmbRegion_query, connection);
        DataTable tableregion = new DataTable();
        daregion.Fill(tableregion);
        cmbRegion.DataSource = tableregion;
        cmbRegion.DisplayMember = "Nameofregions";
        cmbRegion.ValueMember = "ID_region";
        cmbRegion.SelectedIndex = -1;

我试图通过以下代码进行过滤:

    private void cmbHouse_SelectionChangeCommitted(object sender, EventArgs e)
    {
        DataRow selectedDataRow = ((DataRowView)cmbHouse.SelectedItem).Row;
        int num_region = Convert.ToInt32(cmbHouse.SelectedValue);
        string query = "SELECT * FROM Region, House WHERE Region.ID_region='" + num_region.ToString() + "'";
        OleDbDataAdapter danum_region = new OleDbDataAdapter(query, connection);
        DataTable tablenum_region = new DataTable();
        danum_region.Fill(tablenum_region);
        cmbRegion.DataSource = tablenum_region;
        cmbRegion.DisplayMember = "Nameofregions";
        cmbRegion.ValueMember = "ID_region";
    }

当我单击第一个组合框(cmbHouse)中的任何项目时,它显示错误:“ System.Data.OleDb.OleDbException:“条件表达式中的数据类型不匹配”。 我希望第二个组合框(cmbRegion)仅显示房子所在的区域。例如,[house_name]位于该区域。

感谢您的帮助。

0 个答案:

没有答案