wpf 如何在其他 Combobox 的基础上填充一个 Combobox?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16593378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:48:36  来源:igfitidea点击:

How to fill a Combobox based on other Combobox?

c#mysqlwpfcombobox

提问by John Smith

I'm kinda new here, and i have a lot questions for a program I'm making, but the one that bugs me most is the one on the title.

我是新来的,我对我正在制作的程序有很多问题,但最让我烦恼的是标题上的问题。

Basically, I have a Combobox, which I fill using this:

基本上,我有一个组合框,我用这个填充:

DataSet ds = new DataSet();
            MySqlDataAdapter da = new MySqlDataAdapter("SELECT cveestado, nomestado FROM tbEstado", conexion);
            da.Fill(ds, "FillDropDown");
            cbEstado.DisplayMember = "Nomestado";
            cbEstado.ValueMember = "CveEstado";
            cbEstado.DataSource = ds.Tables["FillDropDown"];
            conexion.Close();

And based on whatever the user selects, I want to fill another combobox based on the first selection.

并且基于用户选择的任何内容,我想根据第一个选择填充另一个组合框。

So far I have this, but it does't works:

到目前为止,我有这个,但它不起作用:

private void cbEstado_TextChanged(object sender, EventArgs e)
    {
        if (cbEstado.SelectedValue.ToString() == "Tabasco")
        {
            try
            {
                DataSet ds = new DataSet();
                MySqlDataAdapter da = new MySqlDataAdapter("SELECT cvemunicipio, nommunicipio FROM tbMunicipio where cveestado = 27", conexion);
                da.Fill(ds, "FillDropDown");
                cbMunicipio.DisplayMember = "Nommunicipio";
                cbMunicipio.ValueMember = "Cvemunicipio";
                cbMunicipio.DataSource = ds.Tables["FillDropDown"];
                conexion.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }

Basically, if the user selects "Tabasco", I want the second combobox to fill with the municipalities of Tabasco.

基本上,如果用户选择“塔巴斯科州”,我希望第二个组合框填充塔巴斯科州的自治市。

My sql code is the following, just in case:

我的 sql 代码如下,以防万一:

create table tbEstado
(cveEstado int not null,
nomEstado varchar(45) not null,
constraint pkcveEstado primary key (cveEstado)
)engine=innodb;

create table tbMunicipio
(cveMunicipio int not null,
nomMunicipio varchar(45) not null,
cveEstado int not null,
constraint pkcveMunicipio primary key (cveMunicipio),
constraint fkcveEstado foreign key (cveEstado) references tbEstado(cveEstado)
)engine=innodb;

Thanks!

谢谢!

EDIT

编辑

The answer, thanks to https://stackoverflow.com/users/1197518/steve, is:

感谢https://stackoverflow.com/users/1197518/steve,答案是:

private void cbEstado_TextChanged(object sender, EventArgs e)
        {
            if (cbEstado.SelectedValue != null && Convert.ToInt32(cbEstado.SelectedValue) == 27)
            {
                try
                {
                    DataSet ds = new DataSet();
                    MySqlDataAdapter da = new MySqlDataAdapter("SELECT cvemunicipio, nommunicipio FROM tbMunicipio where cveestado = 27", conexion);
                    da.Fill(ds, "FillDropDown");
                    cbMunicipio.DisplayMember = "Nommunicipio";
                    cbMunicipio.ValueMember = "Cvemunicipio";
                    cbMunicipio.DataSource = ds.Tables["FillDropDown"];
                    conexion.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

采纳答案by Steve

You are checking the SelectedValue property that is linked to the column CveEstado.

您正在检查链接到列的 SelectedValue 属性CveEstado

This column contains numbers not the string Tabasco

此列包含数字而不是字符串 Tabasco

    if (cbEstado.SelectedValue != null && Convert.ToInt32(cbEstado.SelectedValue) == 27)
    {
        try
        {
        .......

This will probably solve your actual problem, but please check also the advices given to you in the comments to your question

这可能会解决您的实际问题,但也请检查您的问题评论中给您的建议

回答by jersoft

You can use dataview for example:

例如,您可以使用数据视图:

cbEstado.Datasource =ds.Tables["State"];
cbMunicipio.Datasource = new Dataview(ds.Tables["Muncipalities"];);

on cbSample1_SelectedIndexChange event you can:

在 cbSample1_SelectedIndexChange 事件上,您可以:

(cbMunicipio.DataSource as DataView).RowFilter = "cveEstado='" +  cbEstado.SelectedValue.ToString() + "'";