Use rdlc Report (2) -- use custom dataset <! -- [If! Supportlists] --> 1 <! -- [Endif] --> Create a form
<! -- [If! Supportlists] --> 2 <! -- [Endif] --> Create a data source
3 <! -- [Endif] --> Create a report
The new data report has been generated. Next, set the data source.
<! -- [If! Supportlists] --> 4 <! -- [Endif] --> set the data source automatically generated by the report
Select a report> data source on the toolbar, select the data source to be modified, and rename it, for example, myds.
After the modification is complete, confirm to exit this window.
Select a table in the report designer to display properties. Change the dataset name of the table to the name modified above.
If a report file contains only one data source, the data value in the table can be directly written as "= Fields! The format of field. value. If it contains multiple data sources, you must specify the value of this field, such as "= (Fields! Field. value, "dataset name "".
<! -- [If! Supportlists] --> 5 <! -- [Endif] --> manually generate a data source
Manually generated datasets must contain the field names designed in the report file. Otherwise, an error occurs.
Generate a dataset:
/// <Summary>
/// Report execution
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Private void button#click (Object sender, eventargs E)
{
// Obtain the dataset
String connstring = "Data Source =.; initial catalog = northwind; Integrated Security = true ";
System. Data. sqlclient. sqlconnection conn1 = new system. Data. sqlclient. sqlconnection (connstring );
System. Data. sqlclient. sqlcommand command1 = new system. Data. sqlclient. sqlcommand ("select * from MERs", conn1 );
System. Data. sqlclient. sqldataadapter ada1 = new system. Data. sqlclient. sqldataadapter (command1 );
Dataset c_ds = new dataset ();
Try
{
Conn1.open ();
Ada1.fill (c_ds );
}
Finally
{
Conn1.close ();
Command1.dispose ();
Conn1.dispose ();
}
// Specify a report file for the report Browser
This. reportviewer1.localreport. reportembeddedresource = "report. report1.rdlc ";
// Specify the dataset. After the dataset name is a table, it is not a dataset of the dataset type.
This. reportviewer1.localreport. CES. Clear ();
This. reportviewer1.localreport. CES. Add (new Microsoft. Reporting. winforms. reportdatasource ("myds", c_ds.tables [0]);
// Display the report
This. reportviewer1.refreshreport ();
}
Data Display after running: