yonan2236
yonan2236

Reputation: 13649

Setting DataSource of a DataGridView control. Am I doing this right? C#

Consider my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ClassTest
{
    public partial class Form1 : Form
    {
        List<Employee> employeeList;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            employeeList = new List<Employee>();
            employeeList.Add(new Employee("000001", "DELA CRUZ, JUAN T."));
            employeeList.Add(new Employee("000002", "GOMEZ, MAR B."));
            employeeList.Add(new Employee("000003", "RIVERA, ERWIN J."));

            dataGridView1.DataSource = employeeList;
        }
    }

    public class Employee
    {
        public Employee(string employeeNo, string name)
        {
            this.employeeNo = employeeNo;
            this.name = name;
        }

        public string employeeNo;
        public string name;
    }
}

I got no output for this...
Where did I go wrong?

Upvotes: 0

Views: 140

Answers (1)

grantnz
grantnz

Reputation: 7423

You need to have public properties for the columns to autogenerate.

Try

    public string employeeNo { get; set; }
    public string name { get; set; }

Upvotes: 1

Related Questions