简体   繁体   中英

Use of a DataGridView with C# and MySQL

A complete noob speaking here. I have a C# interface in which I interact with MySQL. My problem is that I want to show a DataGridView, but I want to change the content of a column. I guess that with code it's more understandable.

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

In CargaEstados() I display in a Combobox (cbEstado) the name (nombre), but I obtain the id (cveestado) {shown below}.

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

In DataGridView I want to to the opposite, with the id, I want to display the name, but I'm not sure how to do that.

My SQL tables are:

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;

FOR INSTANCE, If I have (1, Villahermosa, 27) in tbMunicipios, I want to display (1, Villahermosa, Tabasco).

THANKS :D

This is accomplish able with a SQL join

The statement would look something like this:

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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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