Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,025

HOME > .NET Framework > สร้าง Crystal Report บน Visual Studio (VB.NET , C#) Step by Step




Clound SSD Virtual Server

สร้าง Crystal Report บน Visual Studio (VB.NET , C#) Step by Step

 
  พื้นฐานการสร้างรายงานด้วย Crystal Report บน Visual Studio (VB.NET , C#) เห็นถามกันมาบ่อย ๆ วันนี้มีโอกาสเลยจัดทำเป็นบทความ บทความนี้เป็นพื้นฐานของ Crystal Report ทำงานร่วมกับ .NET Framework ในรูปแบบของ Windows Form Application และฐานข้อมูล SQL Server หรือจะใช้กับ Database อื่น ๆ ก็สามารถใช้ได้เช่นเดียวกัน สามารถสร้างผ่าน Wizard ของ Crystal Report ส่วน Windows Form มีหน้าที่เพียงโหลด Report มาแสดงเท่านั้น

Crystal Report and Visual Studio (VB.NET , C#)


โครงสร้าง ตาราง และข้อมูล ของ SQL Server สำหรับทดสอบ
USE [mydatabase]
GO
/****** Object:  Table [dbo].[customer]    Script Date: 03/13/2012 13:42:18 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[customer](
	[CustomerID] [varchar](4) NOT NULL,
	[Name] [varchar](50) NULL,
	[Email] [varchar](50) NULL,
	[CountryCode] [varchar](2) NULL,
	[Budget] [float] NULL,
	[Used] [float] NULL,
 CONSTRAINT [PK_customer] PRIMARY KEY CLUSTERED 
(
	[CustomerID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

INSERT INTO customer VALUES ('C001', 'Win Weerachai', '[email protected]', 'TH', 1000000, 600000);
INSERT INTO customer VALUES ('C002', 'John  Smith', '[email protected]', 'EN', 2000000, 800000);
INSERT INTO customer VALUES ('C003', 'Jame Born', '[email protected]', 'US', 3000000, 600000);
INSERT INTO customer VALUES ('C004', 'Chalee Angel', '[email protected]', 'US', 4000000, 100000);

GO
SET ANSI_PADDING OFF


นำ Query ไปรันหรือสร้าง Database ตามโครงสร้าง

Crystal Report and Visual Studio (VB.NET , C#)

ตัวอย่างตารางและข้อมูล


มาเริ่มกันเลย

Crystal Report and Visual Studio (VB.NET , C#)

เริ่มต้นด้วยการสร้าง Project ขึ้นมาใหม่ เลือกเป็น Visual Basic (VB.NET) หรือ Visual C# ก็แล้วแต่ถนัด แต่ในส่วนของ Templates ให้เลือกเป็น Windows Form Application


Crystal Report and Visual Studio (VB.NET , C#)

กลับมาที่หน้า Project ให้คลิกขวาที่ Project -> Add -> New Item


Crystal Report and Visual Studio (VB.NET , C#)

เลือก Crystal Report ให้กำหนดชื่อให้เรียบร้อย








Crystal Report and Visual Studio (VB.NET , C#)

จะมีหน้าต่าง Pop ขึ้นให้เลือก Using the Report Wizard และ OK


Crystal Report and Visual Studio (VB.NET , C#)

อันนี้เลือก Create New Connection ส่วน Database ไหนก็ขึ้นอยู่ว่าจะเรียก Report จาก Database อะไร ในที่นี้จะใช้ SQL Server ให้เลือก SQL Native Client


Crystal Report and Visual Studio (VB.NET , C#)

กำหนด Connection เช่น Server, User ID: , Password: , และ Database


Crystal Report and Visual Studio (VB.NET , C#)

เลือก Finish เพื่อข้ามไปขั้นตอนถัดไป


Crystal Report and Visual Studio (VB.NET , C#)

คลิกที่ Connection ที่ได้สร้างไปเมื่อกี่ และคลิกเพื่อเลือกชื่อตารางข้อมูล ซึ่งใน SQL Server จะอยู่ใน Sub ชื่อฐานข้อมูล -> dbo -> Tables -> ชื่อตาราง ส่วนถ้าเป็น Database อื่น ๆ ก็อาจจะแสดงชื่อตารางออกมาเลย


Crystal Report and Visual Studio (VB.NET , C#)

เลือกฟิวด์ Fields ข้อมูลทีต้องการแสดงใน Report และคลิกที่ Finish เพื่อเสร็จสิ้นการออกแบบ แต่ถ้าหากต้องการกำหนดค่าอื่น ๆ ของ Report ลองคลิกที่ Next เพื่อปรับแต่ง Report


Crystal Report and Visual Studio (VB.NET , C#)

หน้าตา Design Report ที่สามารถปรับแต่งรายละเอียดอื่น ๆ ได้ตามความต้องการ


Crystal Report and Visual Studio (VB.NET , C#)

กลับมาที่หน้า Windows Form ให้สร้างปุ่มสำหรับโหลด Report และลาก Control ชื่อ CrystalReportViewer เพื่อใช้สำหรับรองรับการแสดงผลของ Report

จากนั้นให้ Click ที่ Button เพื่อสร้าง Event ในการโหลด Report โดยใส่คำสั่งดังนี้

Code VB.NET
Imports CrystalDecisions.CrystalReports.Engine

Public Class frmReport

    Private Sub btnViewReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnViewReport.Click
        Dim rpt As New ReportDocument()
        Dim directory As String = My.Application.Info.DirectoryPath
        'rpt.Load(directory & "\\myCrystalReport.rpt")
        rpt.Load("C:\DemoCrystalReport\DemoCrystalReport\myCrystalReport.rpt")
        Me.CrystalReportViewer1.ReportSource = rpt
        Me.CrystalReportViewer1.Refresh()
    End Sub

End Class


Code C#
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using CrystalDecisions.CrystalReports.Engine;

public class frmReport
{

	private void btnViewReport_Click(System.Object sender, System.EventArgs e)
	{
		ReportDocument rpt = new ReportDocument();
		string directory = My.Application.Info.DirectoryPath;
		//rpt.Load(directory & "\myCrystalReport.rpt")
		rpt.Load("C:\\DemoCrystalReport\\DemoCrystalReport\\myCrystalReport.rpt");
		this.CrystalReportViewer1.ReportSource = rpt;
		this.CrystalReportViewer1.Refresh();
	}

}


หลังจากวาง Code เรียบร้อยแล้ว ลองกดที่ Run เพื่อดูผลลัพธ์ของ Report

Crystal Report and Visual Studio (VB.NET , C#)

ทดสอบการรัน Report จะได้ผลเหมือนในรูป

บทความนี้เป็นพื้นฐานการใช้ Crystal Report กับ Visual Studio แบบง่าย ๆ สำหรับวิธีการใช้งานในรูปแบบอื่น ๆ เช่น การสร้าง DataSource ด้วย DataSet หรือ DataTable หรือการส่งค่า Parameters ต่าง ๆ จะมีตัวอย่างให้ศึกษาในบทความถัดไป

Download Code!!








บทความอื่น ๆ ที่เกี่ยวข้อง
Go to : สร้าง Parameter และ Formula Fields บน Crystal Reports (VB.NET,C#)
Go to : การสร้าง Crystal Report กับ DataSet หรือ DataTable (VB.NET,C#)
Go to : ASP.NET ReportViewer - rsweb:ReportViewer
Go to : ASP.NET and CrystalReportViewer
Go to : ASP.NET แสดงรูปภาพ Image บน Crystal Report แบบ Step by Step (VB.NET / C#)
Go to : การสร้าง Sub Report (Subreport) บน Crystal Report แบบ Step by Step (VB.NET /C#)


       
Bookmark.   
       

 

  By : TC Admin
  Score Rating : -
  Create Date : 2012-03-13 20:29:15
  Download : No files
     

Clound SSD Virtual Server
-->
Related Links
C# (.Net) Open Excel Template and Create Font,Border,Color,Style (Office 2003,2007,2010)
C# (.Net) Open Excel Template and Create Font,Border,Color,Style (Office 2003,2007,2010)
ตัวอย่างการใช้ C# ในการเปิดไฟล์ Excel ที่มีอยู่ และทำการสร้างเป็น Excel ชุดใหม่ โดยตกแต่งข้อความ เช่น สี , ขนาด ของ ฟอนต์
Rating :
Update :
2017-03-24 21:24:23 View : 21,897
ASP.NET กับแนวคิดการออกแบบ WebPage การวาง Layout ของหน้าเว็บและการเปลี่ยน URL
ASP.NET กับแนวคิดการออกแบบ WebPage การวาง Layout ของหน้าเว็บและการเปลี่ยน URL
บทความนี้เป็นเทคนิคและแนวคิดการวาง Layout โครงสร้างหน้า Webpage บน Web Form ของ ASP.NET ผ่าน Web User Control
Rating :
Update :
2017-03-24 21:15:24 View : 35,420
.NET Smart Device  เขียนโปรแกรมบน Smartphone, Pocket PC , Windows CE , Window  Mobile 5-6, Hand Held,...
.NET Smart Device เขียนโปรแกรมบน Smartphone, Pocket PC , Windows CE , Window Mobile 5-6, Hand Held,...
.NET Smart Device Project เขียนโปรแกรมบน Smartphone, Pocket PC , Windows CE , Window Mobile 5-6, Hand Held,...
Rating :
Update :
2017-03-24 21:17:48 View : 41,405
.NET Console Application เขียนโปรแกรมบน Console Application ด้วย .NET Framework
.NET Console Application เขียนโปรแกรมบน Console Application ด้วย .NET Framework
.NET Console Application เขียนโปรแกรมบน Console Application ด้วย .NET Framework
Rating :
Update :
2017-03-24 21:19:30 View : 41,701
สร้าง Sub Report (Subreport) บน Crystal Report แบบ Step by Step (VB.NET /C#)
สร้าง Sub Report (Subreport) บน Crystal Report แบบ Step by Step (VB.NET /C#)
การสร้าง Sub Report บน Crystal Report และการ Link ข้อมูลระหว่าง Main Report กับ Sub Report รวมทั้งการ Set DataSource ให้กับ Sub Report
Rating :
Update :
2017-03-24 21:34:46 View : 53,634
GridView Ajax and jQuery การสร้าง GridView  บน ASP.NET เพื่อเรียกใช้งาน Ajax กับ jQuery
GridView Ajax and jQuery การสร้าง GridView บน ASP.NET เพื่อเรียกใช้งาน Ajax กับ jQuery
ASP.NET ตัวอย่างการสร้าง GridView และการเรียกใช้งาน Ajax ด้วย jQuery เพื่อเรียกข้อมูลในแต่ละแถวของ GridView แบบง่าย ๆ
Rating :
Update :
2017-03-24 21:30:30 View : 16,595
ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 05
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่

Inline