<! -- [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 operations
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Private VoidButton#click (ObjectSender, eventargs E)
{
// retrieve a dataset
string connstring = " Data Source = .; initial catalog = northwind; Integrated Security = true " ;
System. Data. sqlclient. sqlconnection conn1= NewSystem. Data. sqlclient. sqlconnection (connstring );
System. Data. sqlclient. sqlcommand command1= NewSystem. Data. sqlclient. sqlcommand ("Select * from MERs", Conn1 );
System. Data. sqlclient. sqldataadapter ada1= NewSystem. Data. sqlclient. sqldataadapter (command1 );
Dataset c_ds= NewDataset ();
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 a dataset. The dataset name is followed by a table, datasets not of the dataset type
This . reportviewer1.localreport. datasources. clear ();
This . reportviewer1.localreport. datasources. add ( New Microsoft. reporting. winforms. reportdatasource ( " myds " , c_ds.tables [ 0 ]);
//Show reports
This. Reportviewer1.refreshreport ();
}
Data Display after running: