我有一个带有DataGridView的WinForms表单。 DataGridView是从LINQ to SQL查询填充的,如下所示。当我运行代码时,gridview似乎显示列值而不是实际值。
DataGridView没有设计时源,并且列的DataPropertyName等于数据库中的列。
我错过了什么,它没有显示实际值?
var q = (from r in db.MessagesReceived
orderby r.ID descending
select new { DateSent, DateReceived, ReceivedFrom}).Take(TopNumber);
//dgv.AutoGenerateColumns = false;
dgv.DataSource = q;
dgv.Refresh();
答案 0 :(得分:3)
问题在于选择
select new { DateSent, DateReceived, ReceivedFrom})
应该是
select new { DateSent=r.DateSent, DateReceived=r.DateReceived, ReceivedFrom=r.ReceivedFrom})
您正在选择列而不是值