如何绑定复杂的数据对象的GridView的DataTable? [英] How do I bind a DataTable with complex data objects to a GridView?
问题描述
我的DataTable是一个编程生成,并包含在单元格中键入JobInstance的对象。
My DataTable is programatically generated, and contains objects of type JobInstance in the cells.
DataTable中的dataTable =新的DataTable();
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运算(作业)创建列时(在以// !!!线)时,GridView通过展示JobInstance.ToString在细胞()的结果显示所有列。不过,我有底层JobInstance对象进不去,只有正在显示的字符串值。
When I bind this DataTable to an ASP GridView, it only displays the first column. But if I remove the "typeof(Job)" when creating the column (the line with // !!!), the GridView displays all the columns by showing the result of JobInstance.ToString() in the cells. However, I have no access to the underlying JobInstance object, only the string values that are being displayed.
我需要访问JobInstance对象显示在GridView时(例如在OnRowDataBound),因为我想访问内JobInstance领域的每一个细胞,以确定格式化选项,并添加在每个单元等环节。
I need access to the JobInstance object when displaying the GridView (for example in the OnRowDataBound), because I want to access the fields inside JobInstance for each cell to determine formatting options, and add other links in each cell.
这可能吗?
感谢。
推荐答案
是的,这是可能的。
定义一个新类为您 JobInstance
,把你的领域中它,并为清楚起见让它成为一个单独的文件。
Define a new Class for your JobInstance
and put your fields in it, and for the sake of clarity let it be in a separate file.
在你的aspx code-背后,定义了一个列表< JobInstance>
集合,然后填写它的实例 JobInstance
,那么这个名单分配给数据源
你的`的GridView的。
in your aspx code-behind, define a List<JobInstance>
collection, then fill it in the instances of JobInstance
, then assign this list to the DataSource
of your `GridView'.
要能够访问名单,LT的实例; JobInstance方式&gt;
,使列表是一个字段名成员在你的aspx页面的类
To be able to access the instances of List<JobInstance>
, make the list be a field-member in your aspx page's class.
希望帮助,让我知道如果您需要任何帮助。
Hope that helps, let me know if you need any help.
这篇关于如何绑定复杂的数据对象的GridView的DataTable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!