【问题标题】:Use of a DataGridView with C# and MySQL在 C# 和 MySQL 中使用 DataGridView
【发布时间】:2013-04-14 01:55:24
【问题描述】:

一个完整的菜鸟在这里说话。我有一个与 MySQL 交互的 C# 接口。 我的问题是我想显示一个 DataGridView,但我想更改一列的内容。我想用代码更容易理解。

private void CargaEstados()
    {
        conexion.Open();
        txtNomMun.Focus();
        try
        {
            DataSet ds = new DataSet();
            MySqlDataAdapter da = new MySqlDataAdapter("SELECT cveestado, nombre FROM tbestados", conexion);
            da.Fill(ds, "FillDropDown");
            cbEstado.DisplayMember = "Nombre";
            cbEstado.ValueMember = "CveEstado";
            cbEstado.DataSource = ds.Tables["FillDropDown"];
            conexion.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    private void CargaDataGridView()
    {
        conexion.Open();
        try
        {
            cmd.CommandText = "select cvemunicipio, nombre, cveEstado from tbMunicipios";
            rd = cmd.ExecuteReader();
            while (rd.Read())
            {
                this.dataGridView1.Rows.Add(rd.GetValue(0), rd.GetValue(1), rd.GetValue(2));
            }
            conexion.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

在 CargaEstados() 中,我在 Combobox (cbEstado) 中显示名称 (nombre),但我获得了 id (cveestado) {如下所示}。

"insert into tbmunicipios (nombre, cveestado) values ('" + txtNomMun.Text + "', '" + cbEstado.SelectedValue.ToString() + "')";

在 DataGridView 中我想相反,用 id,我想显示名称,但我不知道该怎么做。

我的 SQL 表是:

Create DataBase CatalogoMun;
use CatalogoMun;
Create table tbEstados
(
   CveEstado int not null,
   Nombre varchar (45) not null,
   Constraint pkCveEstado Primary Key (CveEstado)
)Engine=Innodb;
Create table tbMunicipios
(
    CveMunicipio int not null AUTO_INCREMENT,
    Nombre varchar (45) not null,
    CveEstado int not null,
    Constraint pkCveMunicipio Primary Key (CveMunicipio),
    Constraint fkCVeEdo Foreign Key (CveEstado) references tbEstados (CveEstado)
)Engine=Innodb;

例如,如果我在 tbMunicipios 中有 (1, Villahermosa, 27),我想显示 (1, Villahermosa, Tabasco)。

谢谢:D

【问题讨论】:

    标签: c# mysql winforms datagridview combobox


    【解决方案1】:

    这可以通过SQL join 完成

    语句看起来像这样:

    cmd.CommandText = "SELECT m.cvemunicipio, m.nombre AS NombreA, e.nombre AS NombreB FROM tbMuncipios m INNER JOIN tbEstados e ON m.CveEstado = e.CveEstado";
    

    【讨论】:

    • 它说“'on 子句'中的未知列'm.pkcveestado'”,@Kaiwa
    • 我明白了!只是一点点改变:D
    • cmd.CommandText = "SELECT m.cvemunicipio, m.nombre AS NombreA, e.nombre AS NombreB FROM tbMunicipios m INNER JOIN tbEstados e ON m.CveEstado = e.CVeEstado";
    • 我打错了吗? ;-) 很高兴它起作用了!编辑:啊不,我现在看到了。我的坏;-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2011-08-22
    • 2012-09-26
    • 2014-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多