Yesterday, I wrote C # as the most basic report and manually configured the data source. This article mainly describes how to set report parameters and create sub-tables and groups.
1. Report creation Parameters
Open yesterday's project, create report rptstudent. rdlc in the report folder, drag a table, click Report-Report parameter -- add, parameter name CNO, OK
Second, add a dataset to a subreport.
Still a report -- data source -- add rptdataset_student to the report and confirm.
3. Design subreports
Add group, 1
Figure 1 group is based on classid, including the group header and group end. 2
Figure 2 set = count (Fields! Sno. Value) you can right-click the cell and select the expression -- common functions -- aggregation, select count (), select the field in the brackets of Count (), and double-click Sno. Here, by the way, grouping is actually like an aggregate function in SQL. Based on a certain grouping basis, data is divided into several groups of the same nature, just like using the shift number here, divides student data into several classes. Select a table, right-click the table, select the attribute filter, and set (= Fields! Classid. Value) = (= parameters !. CID. value) 4. settings in the main report. open the main report rptclass, drag a sub-table from the toolbox to the end of the group (it is best to merge one row of cells in the end group), rename it subrptstudent, and set the reportname of the subrptstudent control to rptstudent, right-click -- Property -- parameter -- CID value = Fields! Systemkey. value,
5. Write the Student Data Handler class. First, add the studnet data processing class studentdatasource. class to the datasource file, which provides a data source instance for the report.
public class StudentDataSource
{
public DataTable GetStudentData()
{
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ReportingAPP.Properties.Settings.RptDBConnectionString"].ToString());
StringBuilder cmdText = new StringBuilder();
cmdText.Append("SELECT [SystemKey] ,[Sno],[Sname],[ClassID] FROM [Student]");
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(cmdText.ToString(), conn);
dt.Load(cmd.ExecuteReader());
}
catch (Exception ex)
{
}
finally
{
conn.Close();
}
return dt;
}
}
Then, add the delegate method of local event processing for the subreport to the main form. Add the following method to the main form:
private void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
{
e.DataSources .Add (
new ReportDataSource("RptDataSet_Student", studentDataSource.GetStudentData())
);
}
7. Add the local processing event of the subreport to the main report. In the load event of the main form, add the following statement:
Private void frmrptmain_load (Object sender, eventargs E)
{
This. rptviewmain. localreport. subreportprocessing + =
New subreportprocessingeventhandler (localreport_subreportprocessing); // if no such error exists, an error cannot be displayed in the subreport.
This. rptviewmain. localreport. reportembeddedresource = "reportingapp. Report. rptclass. rdlc ";
This. rptviewmain. localreport. CES. Add (
New reportdatasource ("rptdataset_class", datasource. getclassdata ())
);
This. rptviewmain. refreshreport ();
}
Okay, now I can't wait to press F5 ...... The Student name is not displayed... In the rptstudent table, set datasetname to rptdataset_student and select "yes" as the output data. Now. The running result is as follows:
Now let's simply summarize the processing of the Report. 1) report the rdlc file of the subreport, create parameters, select a dataset, write filter conditions, and select datasetname for the table in the report, set the output data to yes, and set the subreport format and content as needed. 2) Add the subreport control to the main report, and select the reportname of the subreport control as the newly created rdlc subreport, set parameters. 3) provides data instances for writing data to the subreport data source (corresponding datasource class) 4) provides the delegate Processing Method for writing local reports
private void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
{
e.DataSources .Add (
new ReportDataSource("RptDataSet_Student", studentDataSource.GetStudentData())
);
}
5) Add the subreport local processing event this. rptviewmain. localreport. subreportprocessing + =
New subreportprocessingeventhandler (localreport_subreportprocessing); it seems a little tired. Let's do it today and write a drilling report tomorrow.
Yesterday, I wrote C # as the most basic report and manually configured the data source. This article mainly describes how to set report parameters and create sub-tables and groups.
1. Report creation Parameters
Open yesterday's project, create report rptstudent. rdlc in the report folder, drag a table, click Report-Report parameter -- add, parameter name CNO, OK
Second, add a dataset to a subreport.
Still a report -- data source -- add rptdataset_student to the report and confirm.
3. Design subreports
Add group, 1
Figure 1 group is based on classid, including the group header and group end. 2
Figure 2 set = count (Fields! Sno. Value) you can right-click the cell and select the expression -- common functions -- aggregation, select count (), select the field in the brackets of Count (), and double-click Sno. Here, by the way, grouping is actually like an aggregate function in SQL. Based on a certain grouping basis, data is divided into several groups of the same nature, just like using the shift number here, divides student data into several classes. Select a table, right-click the table, select the attribute filter, and set (= Fields! Classid. Value) = (= parameters !. CID. value) 4. settings in the main report. open the main report rptclass, drag a sub-table from the toolbox to the end of the group (it is best to merge one row of cells in the end group), rename it subrptstudent, and set the reportname of the subrptstudent control to rptstudent, right-click -- Property -- parameter -- CID value = Fields! Systemkey. value,
5. Write the Student Data Handler class. First, add the studnet data processing class studentdatasource. class to the datasource file, which provides a data source instance for the report.
public class StudentDataSource
{
public DataTable GetStudentData()
{
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ReportingAPP.Properties.Settings.RptDBConnectionString"].ToString());
StringBuilder cmdText = new StringBuilder();
cmdText.Append("SELECT [SystemKey] ,[Sno],[Sname],[ClassID] FROM [Student]");
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(cmdText.ToString(), conn);
dt.Load(cmd.ExecuteReader());
}
catch (Exception ex)
{
}
finally
{
conn.Close();
}
return dt;
}
}
Then, add the delegate method of local event processing for the subreport to the main form. Add the following method to the main form:
private void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
{
e.DataSources .Add (
new ReportDataSource("RptDataSet_Student", studentDataSource.GetStudentData())
);
}
7. Add the local processing event of the subreport to the main report. In the load event of the main form, add the following statement:
Private void frmrptmain_load (Object sender, eventargs E)
{
This. rptviewmain. localreport. subreportprocessing + =
New subreportprocessingeventhandler (localreport_subreportprocessing); // if no such error exists, an error cannot be displayed in the subreport.
This. rptviewmain. localreport. reportembeddedresource = "reportingapp. Report. rptclass. rdlc ";
This. rptviewmain. localreport. CES. Add (
New reportdatasource ("rptdataset_class", datasource. getclassdata ())
);
This. rptviewmain. refreshreport ();
}
Okay, now I can't wait to press F5 ...... The Student name is not displayed... In the rptstudent table, set datasetname to rptdataset_student and select "yes" as the output data. Now. The running result is as follows:
Now let's simply summarize the processing of the Report. 1) report the rdlc file of the subreport, create parameters, select a dataset, write filter conditions, and select datasetname for the table in the report, set the output data to yes, and set the subreport format and content as needed. 2) Add the subreport control to the main report, and select the reportname of the subreport control as the newly created rdlc subreport, set parameters. 3) provides data instances for writing data to the subreport data source (corresponding datasource class) 4) provides the delegate Processing Method for writing local reports
private void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
{
e.DataSources .Add (
new ReportDataSource("RptDataSet_Student", studentDataSource.GetStudentData())
);
}
5) Add the subreport local processing event this. rptviewmain. localreport. subreportprocessing + =
New subreportprocessingeventhandler (localreport_subreportprocessing); it seems a little tired. Let's do it today and write a drilling report tomorrow.