Mr F
Mr F

Reputation: 33

C# DataGrid not showing Data in WinForm

I have a DataGridView that I am trying to populate as follows:

List<pupil> listOfUsers = new List<pupil>(); // Create a new list 
listOfUsers = pupil.LoadPupilDetails(); // Populate the list with a list of all users 
dgvPupil.DataSource = listOfUsers;

The code works in another project of mine and I followed the same process but it won't appear. The list does return users from a text file. I have also break pointed and can confirm the dgvPupil.DataSource shows this data but it simply won't show on the rendered form.

Initailly, I had this code in the form load event, but I then tried applying it through a manual button click event to no avail.

I would really appreciate some help with this.

Upvotes: 1

Views: 1436

Answers (2)

Mr F
Mr F

Reputation: 33

Sorry for the delay and thank you so much for trying to help.

I usually use encapsulation but for this program, I was aiming for simplicity and was using public properties. So I learned that my class required the properties to have getters and setters so that backing fields would be created at run time. This allowed the data to protrude through to the DGV!

Upvotes: 1

Karen Payne
Karen Payne

Reputation: 5102

See if the simple code sample helps using mocked data. What is needed is to see code that you have as your current code does not really provide enough details.

Note 1 I created the columns for the DataGridView in the designer, set header text and DataPropertyName to match up with each property in the Pupil class.

Note 2 Learn to use a BindingSource.

I would recommend creating a new form devoid of anything other than what is needed to diagnose the current issue.

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace PupilsProject
{
    public partial class Form1 : Form
    {
        private readonly BindingSource _bindingSource = new BindingSource();
        public Form1()
        {
            InitializeComponent();

            dgvPupil.AutoGenerateColumns = false;
            _bindingSource.DataSource = Mocked.Pupils();
            dgvPupil.DataSource = _bindingSource;

            foreach (DataGridViewColumn column in dgvPupil.Columns)
            {
                Console.WriteLine(
                    $"{column.Name}, {column.DataPropertyName}, {column.DefaultCellStyle.Format}");
            }
        }
    }

    class Pupil
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime BirthDate { get; set; }
    }

    class Mocked
    {
        public static List<Pupil> Pupils() =>
            new List<Pupil>
            {
                new Pupil()
                {
                    FirstName = "Jim", 
                    LastName = "Adams", 
                    BirthDate = new DateTime(1990, 1, 2)
                },
                new Pupil()
                {
                    FirstName = "Mary", 
                    LastName = "Jones", 
                    BirthDate = new DateTime(1980, 11, 4)
                },
                new Pupil()
                {
                    FirstName = "Mile", 
                    LastName = "White", 
                    BirthDate = new DateTime(1970,10,9)
                }
            };
    }

}

Output for each DataGridView column

FirstNameColumn, FirstName, 
LastNameColumn, LastName, 
BirthDateColumn, BirthDate, d

Upvotes: 0

Related Questions