简体   繁体   English

如何使用一个组合框在本地数据库的C#中过滤另一个组合的结果

[英]How to use one combo box to filter results for another in C# with a local database

I've spent a good bit of time searching the internet for an answer and can't come up with anything. 我花了很多时间在互联网上寻找答案,却一无所获。 Basically I have a very small database that consists of 5 tables total. 基本上,我有一个非常小的数据库,总共包含5个表。 My problem right now deals with just two of the them though. 我的问题现在只处理其中两个。 I have one table named Model (yes I know I did a bad job at naming this table. Going to try to rename it soon). 我有一个名为Model的表(是的,我知道我在命名该表时做得很糟糕。打算尽快对其重命名)。 Here is what Model looks like. 这是Model的外观。 Model table 型号表

Make ID refers to the unique ID in the table Makes. Make ID是指表Makes中的唯一ID。 Here is what the table Make looks like. 这是Make表的外观。 Make table 制作桌子

I have a windows form app that I created in C# using Visual Studios 2012. This database was created in that project. 我有一个使用Visual Studios 2012在C#中创建的Windows窗体应用程序。此数据库是在该项目中创建的。 I have a form that has among other things, two combo boxes. 我有一个表单,其中有两个组合框。 The first one lists the info from the table Makes. 第一个列出了表格Makes中的信息。 It shows the 3 different car brands. 它显示了3个不同的汽车品牌。 The 2nd combo box shows the different models from the other table. 第二个组合框显示与其他表格不同的模型。 I can get the first combo box to show all the Makes. 我可以得到第一个组合框以显示所有品牌。 I can get the 2nd combo box to show all the Models. 我可以得到第二个组合框来显示所有模型。 But what I want is that if they select Ford in the first box, that it only shows the Fords in the 2nd box. 但是我想要的是,如果他们在第一个框中选择福特,那它只会在第二个框中显示福特。 When they select Ford in the first box, I need to somehow store the unique ID associated with Ford and then use it to filter the 2nd box by referencing the column Make ID in the Model table. 当他们在第一个框中选择福特时,我需要以某种方式存储与福特关联的唯一ID,然后通过引用“模型”表中的“制造ID”列将其用于过滤第二个框。 I've done this in Access, but can't get it to work here. 我已经在Access中完成了此操作,但无法在这里正常工作。 Here is my code I'm using to populate the first box. 这是我用来填充第一个框的代码。

 private void enterNewVehcileForm_Load(object sender, EventArgs e)
    {
        vinAutoPopulateTextBox.Text = mainMenu.VIN;

        mainMenu.connection.Open();

        SqlCeCommand cs = new SqlCeCommand("SELECT * FROM Makes", mainMenu.connection);

        SqlCeDataReader dr = cs.ExecuteReader();
        while (dr.Read())
        {
            vehicleMakeComboBox.Items.Add(dr["Car Brand"]);
        }

        dr.Close();
        dr.Dispose();



        mainMenu.connection.Close();
    }

This populates the first box just fine. 这样就可以填充第一个框。 I see Ford, Chevy, Dodge in the drop down box. 我在下拉框中看到了福特,雪佛兰,道奇。 But if I try this for the 2nd box, it doesn't work. 但是,如果我在第二个盒中尝试此操作,它将无法正常工作。

private void vehicleMakeComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {

        int num = vehicleMakeComboBox.SelectedIndex;

        mainMenu.connection.Open();

        SqlCeCommand modelSearch = new SqlCeCommand("SELECT * FROM Model WHERE [Make ID] = @num", mainMenu.connection);

        SqlCeDataReader modelRead = modelSearch.ExecuteReader();
        while (modelRead.Read())
        {
            vehicleModelComboBox.Items.Add(modelRead["Model"]);
        }

        modelRead.Close();
        modelRead.Dispose();

        mainMenu.connection.Close();

I get an error at the line for while (modelRead.read()) It says a parameter is missing. 我在while (modelRead.read())的行上收到错误,它说缺少参数。

Can anyone help me or point me in the right direction. 谁能帮助我或指出正确的方向。 This is my first time messing with this, so it possible I did everything wrong. 这是我第一次搞砸,所以我可能做错了一切。

You aren't currently providing a value for the parameter @num in you SqlCeCommand . 您目前没有在SqlCeCommand为参数@num提供值。 You can add a value for the parameter like this: 您可以为参数添加一个值,如下所示:

cmd.Parameters.AddWithValue("@num", num)

Here, you are saying that the parameter that you named @num in your SQL is going to have the value of the variable num . 在这里,您说的是您在SQL中命名为@num的参数将具有变量num的值。

在启动ExecuteReader之前,请将以下行添加到代码中。

modelSearch.Parameters.Add("num", SqlDbType.SmallInt).Value = num;

Alright, I got it all working. 好吧,我一切正常。 I realized earlier that my logic was all messed up and I might not have explained myself very well. 我早些时候意识到自己的逻辑混乱了,我可能无法很好地解释自己。 I did get it working, so here it is for anyone else that might find this and have the same issue. 我确实可以正常工作,所以这里适用于可能会发现此问题并且有相同问题的其他任何人。

This part populates the first combo box. 这部分填充第一个组合框。

private void enterNewVehcileForm_Load(object sender, EventArgs e)
    {
        vinAutoPopulateTextBox.Text = mainMenu.VIN;

        mainMenu.connection.Open();

        SqlCeCommand cs = new SqlCeCommand("SELECT * FROM Makes", mainMenu.connection);

        SqlCeDataReader dr = cs.ExecuteReader();
        while (dr.Read())
        {
            vehicleMakeComboBox.Items.Add(dr["Car Brand"]);
        }

        dr.Close();
        dr.Dispose();           

        mainMenu.connection.Close();
    }

This part then filters the 2nd box based on what they picked in the first box. 然后,此部分根据他们在第一个框中选择的内容过滤第二个框。

        private void vehicleMakeComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        string ID = null;
        string command = "SELECT * FROM Makes WHERE [Car Brand] = '" + vehicleMakeComboBox.Text + "'";
        string command2 = null;

        mainMenu.connection.Open();

        SqlCeCommand makeSearch = new SqlCeCommand(command, mainMenu.connection);

//  This part gets the ID of the car brand they picked in the first combo box.  Ford is 1, Chevy is 2, Dodge is 3  
        SqlCeDataReader makeRead = makeSearch.ExecuteReader();
        while (makeRead.Read())
        {
            ID = (makeRead["ID"].ToString());
        }

        makeRead.Close();
        makeRead.Dispose();

        vehicleModelComboBox.Items.Clear(); // Clears the combo box incase they picked a brand and then picked another

//  This part now selects all rows in the Model table that have the same value in the Make ID column as the car brand they chose in the first combo box  
        command2 = "SELECT * FROM Model WHERE [Make ID] = " + ID;

        SqlCeCommand modelSearch = new SqlCeCommand(command2, mainMenu.connection);

        SqlCeDataReader modelRead = modelSearch.ExecuteReader();
        while (modelRead.Read())
        {
            vehicleModelComboBox.Items.Add(modelRead["Model"]);
        }

        modelRead.Close();
        modelSearch.Dispose();

        mainMenu.connection.Close();
    }

Thank you to @TomDoesCode and @Kami. 感谢@TomDoesCode和@Kami。 You guys got me thinking in the right direction which made me see where my code was lacking. 你们让我朝着正确的方向思考,这使我看到了我的代码缺乏的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM