(1)先建立一个实体类,拉出datagridview控件,点击右上角一个三角形,选择添加项目源,选上刚刚建立的Students的类,确认完成。.



(3)最后是加入数据,先实例化一个students类的集合,将集合自己赋值给datagridview的数据源,数据就会出现在表格中了,如下图。

代码展示:
using System;using System.Collections.Generic;using System.Windows.Forms;namespace DataGridView绑定类对象{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){List<Students> students = new List<Students>();students.Add(new Students { Name="李增光",Age=18,Sex="男"});//直接将这个学生对象的集合设置为Datagridview的数据源dataGridView1.DataSource = students;}}public class Students{public string Name { get; set; }public int Age { get; set; }public string Sex { get; set; }}}
本文案例的源码放在下面的链接中,需要可以自取。
链接:https://pan.baidu.com/s/1Qjc8Cal9STbqEtVPJ_ywwQ
提取码:7fxt