0

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

1 Answer 1

1

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";
Sign up to request clarification or add additional context in comments.

4 Comments

It says "unknown column 'm.pkcveestado' in 'on clause'", @Kaiwa
I GOT IT! just a little change :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";
Did I make a typo? ;-) Glad it worked tho! Edit: Ah no I see it now. My bad ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.