如何根据其他组合框中的选择过滤组合框?

时间:2019-05-23 07:59:57

标签: c# database winforms combobox oledb

我有3张桌子:

1)房子:

| ID_house | House_names | num_region |
---------------------------------------
|   int    |    names    |    int     |

num_region-外键,它等于Region表中的主键(Region.ID_region)。

2)地区:

| ID_region | Region_names | num_area |
---------------------------------------
|   int     |    names     |   int    |

num_area-外键,等于Areas_InRegion表中的主键(Areas_InRegion.ID_area)。

3)Areas_InRegion:

| ID_area | Area_names |
------------------------
|   int   |    names   |

我有3个组合框:

1)cmbHouse-用于显示“房屋”表中房屋的名称。

2)cmbRegion-用于显示“区域”表中区域的名称。

3)cmbArea-用于显示Areas_InRegion表中区域的区域名称。

我像这样填充这些组合:

        //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";
        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 = "Region.ID_region";
        cmbRegion.SelectedIndex = -1;

        //cmbArea
        string cmbArea_query = "SELECT * FROM Areas_InRegion";
        OleDbDataAdapter daArea = new OleDbDataAdapter(cmbArea_query, connection);
        DataTable tablearea = new DataTable();
        daArea.Fill(tablearea);
        cmbArea.DataSource = tablearea;
        cmbArea.DisplayMember = "Names_OfAreas";
        cmbArea.ValueMember = "Areas_InRegion.ID_areas";
        cmbArea.SelectedIndex = -1;

我可以通过cmbHouse组合中的选择过滤cmbRegion组合:

    private void cmbHouse_SelectionChangeCommitted(object sender, EventArgs e)
    {
        if (cmbHouse.SelectedIndex != null)
        {
            int num_region = Convert.ToInt32(cmbHouse.SelectedValue);
            OleDbCommand com = new OleDbCommand();
            com.CommandText = "SELECT * FROM Region WHERE Region.ID_region=" + num_region.ToString() + "";
            com.Parameters.AddWithValue("House.num_region", typeof(int));
            OleDbDataAdapter danum_region = new OleDbDataAdapter(com.CommandText, connection);
            DataTable tablenum_region = new DataTable();
            danum_region.Fill(tablenum_region);
            cmbRegion.DataSource = tablenum_region;
            cmbRegion.DisplayMember = "Nameofregions";
            cmbRegion.ValueMember = "Region.ID_region";
            cmbRegion.SelectedIndex = -1;    
        }  
    }

但是当我尝试在过滤cmbArea组合中过滤cmbRegion代码时,出现错误“没有为一个或多个必需参数提供值”。在这里:

    private void cmbRegion_SelectionChangeCommitted(object sender, EventArgs e)
    {            
        int num_area = Convert.ToInt32(cmbRegion.SelectedValue);
        OleDbCommand com2 = new OleDbCommand();
        com2.CommandText = "SELECT * FROM Areas_InRegion WHERE Region.num_arearegion=" + num_area.ToString() + "";
        com2.Parameters.AddWithValue("Region.num_arearegion", typeof(int));
        OleDbDataAdapter danum_area = new OleDbDataAdapter(com2.CommandText, connection);
        DataTable tablenum_area = new DataTable();
        danum_area.Fill(tablenum_area); //Error 'No value given for one or more required parameters.' appears here
        cmbArea.DataSource = tablenum_area;
        cmbArea.DisplayMember = "Names_OfAreas";
        cmbArea.ValueMember = "Areas_InRegion.ID_areas";
        cmbArea.SelectedIndex = -1;
    }

帮我解决这个问题。谢谢您的帮助。

0 个答案:

没有答案