1

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

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

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();

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

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

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);
            }
        }
    }

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

我的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;

谢谢!

编辑

感谢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);
                }
            }
        }
4

2 回答 2

0

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

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

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

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

于 2013-05-16T17:14:31.290 回答
0

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

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

在 cbSample1_SelectedIndexChange 事件中,您可以:

(cbMunicipio.DataSource as DataView).RowFilter = "cveEstado='" +  cbEstado.SelectedValue.ToString() + "'";
于 2013-05-17T06:25:52.077 回答