Note that there are some explanatory texts on larger screens.

plurals
  1. POUse of a DataGridView with C# and MySQL
    primarykey
    data
    text
    <p>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.</p> <pre><code>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); } } </code></pre> <p>In CargaEstados() I display in a Combobox (cbEstado) the name (nombre), but I obtain the id (cveestado) {shown below}. </p> <pre><code>"insert into tbmunicipios (nombre, cveestado) values ('" + txtNomMun.Text + "', '" + cbEstado.SelectedValue.ToString() + "')"; </code></pre> <p>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.</p> <p>My SQL tables are:</p> <pre><code>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; </code></pre> <p>FOR INSTANCE, If I have (1, Villahermosa, 27) in tbMunicipios, I want to display (1, Villahermosa, Tabasco).</p> <p>THANKS :D</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload