I'm populating a DataGridView
object with a list of classes of type List<Curso>
, and the way that I populate DataGridView
is as follows:
meuDataGridView.DataSource = cursos;
However, one of the fields in my class Curso
is another class, which in this case is the Instituicao
class. And in the data view in the grid the Nome
field of the Instituicao
class does not appear correctly.
Here is an illustration of the data on the grid:
Reproducingtheminimumexample
Inordertoreproducetheminimalexampleoftheproblem,youwillneedtheclassCurso
ofclassInstituicao
andaformwithDataGridView
.
ClassCourse:
classCurso{publicstringDescricao{get;set;}publicintCargaHoraria{get;set;}publicInstituicaoInstituicao{get;set;}}
InstitutionClass:
publicclassInstituicao{publicstringNome{get;set;}}
FormclasswithDataGridView
anddatapopulatedinit:
publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidForm1_Load(objectsender,EventArgse){varcursos=newList<Curso>{newCurso{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 the value of the property Nome
of class Instituicao
on the grid instead of that unusual value?