3
I’m showing the data from a table(TB_LIBERACAO
) in a DataGrid
, however this table has 2 foreign keys from the 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
id’s(numbers) will appear, but I would like the value of the board fields to appear(tbCarro
) and name(tbMotorista
).
I can do it in the database, but I’m not getting through 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 plays the objects to a list and returns this 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();
}
}
Thank you very much Maycon, I had to make a change in the code, but you solved 99.9% of the problem, I will post below the changes:
– Junior
Mass, happy to have helped, anything just talk ;)
– Maycon F. Castro