14
我正在尋找使用通用列表加載GridView並自動生成列。我收到一個異常,它沒有正確的屬性來允許它自動生成列。使用通用列表作爲數據源和自動生成列的GridView
異常
The data source for GridView with id 'GV1' did not have any properties or attributes from which to generate columns. Ensure that your data source has content.
的GridView
<asp:GridView ID="GV1" runat="server" AutoGenerateColumns="true"></asp:GridView>
網頁加載
//LINQ query to populate list
List<student> su = new List<student>();
dbDataContext db = new dbDataContext();
var q = from c in db.data_table
where c.processed == false
orderby c.date_complete descending
select c;
//iterate through results and add to list
foreach(var c in q)
{
student s = new student { name = c.name, address = c.address };
su.Add(s);
}
//Load GridView
GV1.DataSource = su;
GV1.DataBind(); //Exception thrown here
學生班級
public class student
{
public string name;
public string address;
}
任何想法或建議表示讚賞,隨時讓我知道,如果我要對此完全錯誤的。
這回答了類似的問題,我有。我不明白的是爲什麼它有效。爲什麼我可以手動訪問'student.name',但是除非添加訪問器,否則'GridView'不能完成它? – Brendan 2012-11-14 17:34:10
我不能特別說,我只能告訴你,網格只會訪問屬性而不是字段。我可以*猜測*當它反射時,它反映到公共屬性('BindingFlags.GetProperty')而不是公共字段('BindingFlags.GetField')。 – CodingGorilla 2012-11-14 19:30:35