getting reference to the feature class table in a report script
I’m writing a Script for a new report (with subreports) and I’d like to get a reference to the “default” table (DataTable[0] in the DataSet) that is passed to the subreport. The subreport already displays fields in my feature class by using the standard method of using the field name in the DataField property of various Label objects, so it’s obviously getting the DataTable correctly from the main report. But I have a row of totals as the last row in my DataTable[0] (that I calculated in a report extension) and I’d like to access those values by doing something like this in Detail1_Format:
public void Detail1_Format(DataSet ds)
{
DataTable myTable = ds.Tables["MyFeatureClassName"];
Label totalMyLabel = (Label)rpt.Sections["Detail1"].Controls["MyReportTotalLabelName"];
totalMyLabel.Text = myTable.Rows[myTable.Rows.Count-1][“MyColumnName”];
. . .
}
I’ve actually tried code very similar to that above but I’m (understandably) getting a “parameter count mismatch”, as Detail1_Format probably doesn’t have any overloaded versions that accept a DataSet.
Any thoughts on how to get a reference to the first DataTable in my GTX DataSet?
-
Hi Chris,
This is tricky - When you add a subreport to your report template, Essentials will associate one of the DataTables in the DataSet with that report (only one). So, when the subreport runs, its data source is a single table.
You may be able to reassign the data source for the subreport in the Detail1_Format event in the parent report to use the entire DataSet - then in the SubReport, you would have access to all the same tables as were in the toplevel report.
Regards,
-Malcolm
0 -
You don't need to pass the data table to the format method, as it is already there. You just need to know that you can access it via:
DataSet reportData = (DataSet)rpt.DataSource;
or
DataTable subreportData = (DataTable)rpt.DataSource;
Depending on whether you are in the report or subreport. I think from your description you would like to access the datatable sent to the subreport from within the subreport's Detail_Format event, so you'll need the second form, and then you will get a direct reference to the correct data table.
Also note that if you actually want to manipulate or change the DataTable or DataSet that you need to do this in the ActiveReport_DataInitialize event. This will run before any code in Detail1_Format. Manipulating the data anywhere else can have unpredictable results.
0 -
Thanks all.
Malcom, I probably wasn't clear enough in my description: I only needed to get a reference to the DataTable that is already passed to the subreport, not a different DataTable in the DataSet. rpt.DataSource was what I needed (thanks Jonathan). Interestingly, the rpt.DataSource in my subreport was the complete DataSet, not the DataTable usually associated with the subreport (i.e. DataSet.Tables[0]), so I had to do:
DataSet reportData = (DataSet)rpt.DataSource;
DataTable myTable = (DataTable)reportData.Tables[0];Otherwise if I try to do 'DataTable myTable = (DataTable)rpt.DataSource;' then I get an 'invalid cast exception'.
Thanks again all, much appreciated.
0
Please sign in to leave a comment.
Comments
3 comments