Step 1: Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "PrintProducts" and then click OK
Step 2: Design your form as below
Main form: Form1

Print form: frmPrint

Step 3: Design your report as below

Step 4: You need create a stored procedure to retrieve product data from the Northwind database
create procedure [dbo].[GetProductList]
as
select ProductID, ProductName, QuantityPerUnit, UnitPrice, UnitsInStock
from Products
If you haven't got Northwind database, you can view How to download and restore Northwind database to Sql Server
Step 5: Create an Entity Framework Model First, then add the GetProductList to your EF Model
Add code to handle Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity.Core.Objects;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PrintProducts
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnPrint_Click(object sender, EventArgs e)
{
//Get data from data source, then open print form dialog
List<GetProductList_Result> list = getProductListResultBindingSource.DataSource as List<GetProductList_Result>;
if (list != null)
{
using (frmPrint frm = new frmPrint(list))
{
frm.ShowDialog();
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
//Load data to data source
using(NorthwindEntities db = new NorthwindEntities())
{
getProductListResultBindingSource.DataSource = db.GetProductList().ToList();
}
}
}
}
Add code to handle frmPrint
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PrintProducts
{
public partial class frmPrint : Form
{
List<GetProductList_Result> list;
//Passing parameters to constructor
public frmPrint(List<GetProductList_Result> list)
{
InitializeComponent();
this.list = list;
}
private void frmPrint_Load(object sender, EventArgs e)
{
//Get data from constructor
GetProductList_ResultBindingSource.DataSource = list;
this.reportViewer1.RefreshReport();
}
}
}
VIDEO TUTORIALS