Reputation: 27
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
Upvotes: 0
Views: 282