// 从此 Admin 类获取数据:
public static IQueryable<Student> GetStudents()
{
DojoDBDataContext conn = new DojoDBDataContext();
var query =
from s in conn.Students
join b in conn.Belts on s.BeltID equals b.ID
orderby s.LastName ascending
select s;
return query;
}
// 在我的表单上:
BindingSource bs = new BindingSource();
private void fillStudentGrid()
{
bs.DataSource = Admin.GetStudents();
dgViewStudents.DataSource = bs;
dgViewStudents.Columns.Remove("ID");
}
工作得很好,但与其删除我不想要的 20 多列数据,我宁愿只添加我所做的少数数据。另外,命名标题标题是一种奖励。但是, add 方法对我不起作用:
private void fillStudentGrid()
{
bs.DataSource = Admin.GetStudents();
dgViewStudents.AutoGenerateColumns = false;
dgViewStudents.DataSource = bs;
dgViewStudents.Columns.Add("ID", "ID Number");
}
我得到了适当的行数,并且列标题设置正确……但是行中填充了空白数据。