工具箱使用dataGridView(表格)
右键属性 找到Columns(集合)
添加 页眉文本更改名称;类型可以设置表格类型
代码:
设置不可编辑
dataGridView1.ReadOnly = true;
取消自动创建列
dataGridView1.AllowUserToAddRows = false;
添加数据
创建
List<Student> list = new List<Student>();
cs
public Form1()
{
List<Student> list = new List<Student>();
list = new List<Student>();
for (int i = 0; i < 10; i++)
{
list.Add(new Student()
{
Name = "女朋友" + i + "号",
Age = i * 10,
Sex = i % 2 == 0 ? "男" : "女",
Job = "开挖掘机",
});
}
dataGridView1.DataSource = ToDataTable(list);
}
作业表格展示学生三门成绩 最后展示总成绩
类添加三个科目
方法1:
for (int i = 0; i < 10; i++)
{
Student s1 = new Student();
s1.Name = "女朋友" + i + "号";
s1.Age = i * 10;
s1.Sex = i % 2 == 0 ? "男" : "女";
s1.Job = "开挖掘机";
s1.tiyu = i * 2;
s1.sql = i * 3;
s1.cshap = i * 4;
s1.sum = s1.tiyu+s1.sql+s1.cshap;
list.Add(s1);
dataGridView1.DataSource = ToDataTable(list);
}
方法2:
for (int i = 0; i < 10; i++)
{
list.Add(new Student()
{
Name = "女朋友" + i + "号",
Age = i * 10,
Sex = i % 2 == 0 ? "男" : "女",
Job = "开挖掘机",
tiyu = i * 2,
sql = i * 3,
cshap = i * 4,
sum = i * 2 + i * 3 + i * 4
});
dataGridView1.DataSource = ToDataTable(list);
}
只有添加累的循环