2
I have a Datagridview that should show data between related tables and I can’t get it to show the data correctly, it shows the namespace name followed by the class name. I saw some things and showed an override, but I also read that it is not good practice and suggested to do through the event cellFormating.
What happens to me is this:
This class is in the DTO namespace
public class Despesa
{
public int id_despesa { get; set; }
public Categoria categoria { get; set; }
public string descricao { get; set; }
public decimal valor { get; set; }
public Mes mes { get; set; }
public string previsao { get; set; }
public Forma_Pagamento forma_Pagamento { get; set; }
public Periodo periodo { get; set; }
}
Then my expense collection will inherit a list of expenses
public class DespesaCollection : List<Despesa>
{
}
In the business layer I put all expenses within the collection
foreach (DataRow linha in DataTableDespesas.Rows)
{
Despesa despesa = new Despesa();
despesa.id_despesa = Convert.ToInt32(linha["id_despesa"]);
despesa.categoria = new Categoria();
despesa.categoria.id_categoria = Convert.ToInt32(linha["id_categoria"]);
despesa.descricao = linha["descricao"].ToString();
despesa.valor = Convert.ToDecimal(linha["valor"]);
despesa.mes = new Mes();
despesa.mes.id_mes = Convert.ToInt32(linha["id_mes"]);
despesa.previsao = linha["previsao"].ToString();
despesa.forma_Pagamento = new Forma_Pagamento();
despesa.forma_Pagamento.id_forma_pagamento = Convert.ToInt32(linha["id_formaPagamento"]);
despesa.periodo = new Periodo();
despesa.periodo.id_periodo = Convert.ToInt32(linha["id_periodo"]);
despesaCollection.Add(despesa);
}
And here’s how I load the collection to display on dataGridView.
private void carregarDataGridView()
{
DespesaCollection despesaCollection = new DespesaCollection();
DespesaNegocio despesaNegocio = new DespesaNegocio();
despesaCollection = despesaNegocio.ConsultarTudo();
dataGridView1.DataSource = null;
dataGridView1.DataSource = despesaCollection;
dataGridView1.Refresh();
}
Show how you are giving bind on the grid. The solution I have, several including.
– Jéf Bueno
I already edited the question and left everything necessary so you can show me how it’s done, thanks!!! @jbueno
– Diogo Sousa
@jbueno I was very curious to know what solutions I had for my case!
– Diogo Sousa
I posted an answer, Diogo. I hope it’s useful.
– Jéf Bueno