Resolved DataGridView not showing values from object

Prika

New member
Joined
Aug 25, 2023
Messages
1
Programming Experience
Beginner
Hi,
I'm new to programming and I'm stuck.
I want to show the name and surename of the generated duos in a datagrid.
No matter what I try: the columnheaders are created, but the names do not show.
Help is much appreciated.

C#:
        private void btnCalculate_Click(object sender, EventArgs e)
        {
            List<Person> people = new List<Person>()
            {
            new Person { Name = "A", Surname = "F", Id = 1},
            new Person { Name = "B", Surname = "G", Id = 2},
            new Person { Name = "C", Surname = "H", Id = 3},
            new Person { Name = "D", Surname = "I", Id = 4},
            new Person { Name = "E", Surname = "J", Id = 5},
            };

            
            List<Duo> duos = new List<Duo>();
            for (int i = 0; i < people.Count; i++)
            {
                Duo duo = new Duo();
                duo.Person1 = people[i];

                if (i + 1 < people.Count)
                    duo.Person2 = people[i + 1];
                duos.Add(duo);
            }
            //dataGridDuo.DataSource = people;


            dataGridDuo.AutoGenerateColumns = false;

            
            DataGridViewTextBoxColumn person1NameColumn = new DataGridViewTextBoxColumn();
            person1NameColumn.HeaderText = "Person1 Name";
            person1NameColumn.DataPropertyName = "Person1.Name";

            DataGridViewTextBoxColumn person1SurenameColumn = new DataGridViewTextBoxColumn();
            person1SurenameColumn.HeaderText = "Person1 Surename";
            person1SurenameColumn.DataPropertyName = "Person1.Surname";

            
            DataGridViewTextBoxColumn person2NameColumn = new DataGridViewTextBoxColumn();
            person2NameColumn.HeaderText = "Person2 Name";
            person2NameColumn.DataPropertyName = "Person2.Surname";

            DataGridViewTextBoxColumn person2SurnameColumn = new DataGridViewTextBoxColumn();
            person2SurnameColumn.HeaderText = "Person2 Surname";
            person2SurnameColumn.DataPropertyName = "Person2.Surname";

            dataGridDuo.Columns.Add(person1NameColumn);
            dataGridDuo.Columns.Add(person1SurenameColumn);
            dataGridDuo.Columns.Add(person2NameColumn);
            dataGridDuo.Columns.Add(person2SurnameColumn);

            dataGridDuo.DataSource = duos;
        }
 
Are you sure that the Data grid view's DataPropertyName supports navigating an object graph where you have "parentPropertyName.childPropertyName"? WPF supports it in its data binding, but I'm unsure if the WinForms controls do. Recall that WinForms was many years before WPF so the binding concepts and implementations are different.

If WinForms Data Grid View does not support such an object graph path walking, them you will need to expose another property on the parent object that does the path walking for it. Something like:
C#:
class Duo
{
    :
    public string Person1Name => Person1.Name;
    public string Person1Surname => Person1.Surname;
    public string Person2Name => Person2.Name;
    public string Person2Surname => Person2.Surname;
    :
}
:
person1NameColumn.DataPropertyName = "Person1Name";
person1SurnameColumn.DataPropertyName = "Person1Surname";
person2NameColumn.DataPropertyName = "Person2Name";
person2SurnameColumn.DataPropertyName = "Person2Surname";
:
 
Last edited:

Latest posts

Back
Top Bottom