I am displaying the data of a table ( TB_LIBERACAO
) in a DataGrid
, but this table has 2 foreign keys coming from tables TB_CARROS
and TB_MOTORISTAS
TB_LIBERACAO
idLib
idCarro(FK)
idMotorista(FK)
TB_CARRO
idCarro
placa
TB_MOTORISTAS
idMotorista
nome
In the fields idCarro
and idMotorista
will appear the id's, but I would like the value of the fields pla ( tbCarro
) and name ( tbMotorista
) to appear.
I can do it in the database, but I can not get to C #
public void atualizarGrid() {
LiberacoesControle liberacoesControle = new
LiberacoesControle();
dataGridView1.DataSource = null;
dataGridView1.DataSource = liberacoesDAL.preencherGrid();
dataGridView1.Update();
dataGridView1.Refresh();
}
I have a method that throws objects to a list and returns that list
(dataGridView1.DataSource = liberacoesDAL.preencherGrid();)
public List<LiberacoesModelo> preencherGrid()
{
LiberacoesModelo liberacao = null;
MySqlCommand comandoSql = null;
try
{
abrirConexao();
List<LiberacoesModelo> listaLiberacao = new List<LiberacoesModelo>();
String sql = "Select * from liberacoes";
comandoSql = new MySqlCommand(sql, conexao);
comandoSql.Parameters.Clear();
//comandoSql.Parameters.Add("@id", MySqlDbType.String).Value = id;
MySqlDataReader dr = comandoSql.ExecuteReader();
while (dr.Read())
{
liberacao = new LiberacoesModelo();
liberacao.idliberacoes = dr.GetInt32(dr.GetOrdinal("idLiberacao"));
liberacao.idcarro = dr.GetInt32(dr.GetOrdinal("idCarro"));
liberacao.idmotorista = dr.GetInt32(dr.GetOrdinal("idMotorista"));
liberacao.dataLiberacoes = dr.GetString(dr.GetOrdinal("dataLiberacao"));
listaLiberacao.Add(liberacao);
}
dr.Close();
return listaLiberacao;
}
catch (Exception erro)
{
throw erro;
}
finally
{
fecharConexao();
}
}