1
I’m populating an object DataGridView
with a list of type classes List<Curso>
, and the way I populate the DataGridView
is as follows:
meuDataGridView.DataSource = cursos;
But one of the fields of my class Curso
is another class, which in this case is the class Instituicao
. And in the data display on the grid the field Nome
class Instituicao
does not appear correctly.
Here is an illustration of the data in the grid:
Reproducing the minimum example
To reproduce the minimum example of the problem illustration, you will need the class Curso
class Instituicao
and a form with DataGridView
.
Class Course:
class Curso
{
public string Descricao { get; set; }
public int CargaHoraria { get; set; }
public Instituicao Instituicao { get; set; }
}
Class Institution:
public class Instituicao
{
public string Nome { get; set; }
}
Class of the form with the DataGridView
and data populated in it:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
var cursos = new List<Curso>
{
new Curso
{
Descricao = "Curso Java",
CargaHoraria = 99999999,
Instituicao = new Instituicao
{
Nome = "Instituicao Javali"
}
},
new Curso
{
Descricao = "Curso Python",
CargaHoraria = 1,
Instituicao = new Instituicao
{
Nome = "Monty Python"
}
},
new Curso
{
Descricao = "Curso de PHP",
CargaHoraria = -99999999,
Instituicao = new Instituicao
{
Nome = "Instituicao no fim da galaxia"
}
}
};
PopulaGrid(cursos);
}
private void PopulaGrid(List<Curso> cursos)
{
if (cursos != null && cursos.Any())
{
dataGridView.DataSource = cursos;
}
}
}
Question
How can I display property value Nome
class Instituicao
on the grid instead of that unusual value?
new Curso { Descricao = "Curso Java", CargaHoraria = 99999999, Nome = "Instituicao Javali" }
?– Maniero
@Maniero the class
Instituicao
has more fields, this is just one example :)– gato