我的DataTable以编程方式生成,并在单元格中包含JobInstance类型的对象。如何将具有复杂数据对象的DataTable绑定到GridView?
DataTable dataTable = new DataTable();
// first column - label
dataTable.Columns.Add("JobType", typeof(string));
// other columns (data-driven)
foreach(string config in Configurations)
{
DataColumn col = new DataColumn(config, typeof(JobInstance)); // !!!
dataTable.Columns.Add(col);
}
// rows
foreach(string jobType in JobTypes)
{
DataRow row = dataTable.NewRow();
row["JobType"] = jobType;
foreach(string config in Configurations)
{
row[config] = GetJobInstance(jobType, config); // returns a JobInstance object
}
dataTable.Rows.Add(row);
}
当我结合这个数据表到ASP的GridView,它仅显示第一列。但是,如果在创建列(行// !!!)时删除“typeof(Job)”,则GridView通过显示单元格中JobInstance.ToString()的结果显示所有列。但是,我无法访问底层的JobInstance对象,只有正在显示的字符串值。
我需要在显示GridView时访问JobInstance对象(例如在OnRowDataBound中),因为我想访问JobInstance中每个单元格的字段以确定格式选项,并在每个单元格中添加其他链接。
这可能吗?
谢谢。
嗯,我不想显示JobInstance的字段作为列。我的列是独立定义的,每个单元格都是一个JobInstance。但最后我做了类似于你所建议的事情,并直接在行/列中访问我的数据结构结构中的对象,因为似乎没有办法按照我想要的方式实际将对象绑定到gridview。 – ewerx 2011-02-02 21:15:22