0
votes

My DataTable is programatically generated, and contains objects of type JobInstance in the cells.

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);    
}

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.

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.

Is this possible?

Thanks.

1

1 Answers

1
votes

Yes, This is possible.

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.

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'.

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.