繁体   English   中英

设置DataGridView控件的DataSource。 我这样做对吗? C#

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

考虑我的代码:

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;
    }
}

我对此没有输出...
我哪里做错了?

您需要具有公共属性,列才能自动生成。

尝试

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM