Add Crystal Report Viewer to the window form.
- Resize "Form1", and add "CrystalReportViewer" to the form, with the default setting unchanged.
view image
- Add a reference to BarcodeLib.Barcode.dll
- Add the following code to the Form1.cs file.
using System.Data.OleDb;
using System.Drawing.Imaging;
using BarcodeLib.Barcode.Linear;
- Add the following code to the Form1.cs file.
private void Form1_Load(object sender, EventArgs e)
{
//create the database connection. Please change to correct data file (BarcodeDemoData.mdb) path.
OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/net_barcode_trial/Reporting/BarcodeDemoData.mdb");
aConnection.Open();
OleDbDataAdapter dataAdapter = new OleDbDataAdapter("SELECT * FROM Customer", aConnection);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
//Add the Barcode column to the DataSet
ds.Tables[0].Columns.Add(new DataColumn("Barcode", typeof(byte[])));
//Create an instance of Linear Barcode
//Use BarcodeLib.Barcode.DataMatrix.DataMatrix for Data Matrix
//Use BarcodeLib.Barcode.PDF417.PDF417 for PDF417
//Use BarcodeLib.Barcode.PDF417.MicroPDF417 for MicroPDF417
BarcodeLib.Barcode.Linear.Linear barcode = new BarcodeLib.Barcode.Linear.Linear();
//Barcode settings
barcode.Type = BarcodeLib.Barcode.Linear.BarcodeType.CODE128;
barcode.BarHeight = 50; //50 pixel
barcode.Format = ImageFormat.Png;
foreach (DataRow dr in ds.Tables[0].Rows)
{
barcode.Data = (int)dr["CustomerId"] + "";
byte[] imageData = barcode.drawBarcodeAsBytes();
dr["Barcode"] = imageData;
}
CustomerReport rpt = new CustomerReport();
rpt.SetDataSource(ds);
this.crystalReportViewer1.ReportSource = rpt;
//close the connection Its important.
aConnection.Close();
}