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,027

HOME > .NET Framework > การสร้าง Report ด้วย Report Viewer และการส่งค่า Parameters (ReportViewer , MicrosoftReportViewer)




Clound SSD Virtual Server

การสร้าง Report ด้วย Report Viewer และการส่งค่า Parameters (ReportViewer , MicrosoftReportViewer)

 
  การสร้าง Report ด้วย Report Viewer และการส่งค่า Parameters (ReportViewer , MicrosoftReportViewer) สำหรับ Report Viewer เป็น Control อยู่ที่อยู่ในกลุ่มของ Reporting ที่มีมาพร้อมกับ Visual Studio 2005 (.NET 2.0) ขึ้นไป โดยใน 2005 และ 2008 จะชื่อ MicrosoftReportViewer แต่ใน Visual Studio 2010 จะชื่อ ReportView ทำงานแสดงผลได้ทั้งบน Web(Asp.net) หรือ Win App(Windows Form Application) เป็น Control ที่ช่วยในการจัดการออก Report ของค่าย Microsoft การทำงานจะคล้าย ๆ กับ Crystal Report คือจะต้องสร้างไฟล์ Report ซึ่งมีนามสกุล .rdlc และสร้าง DataSource Connection สำหรับการดึงข้อมูลจาก Database หรือแหล่งอื่น ๆ สำหรับการสร้าง DataSource มีทั้งแบบ Wizard หรือแบบสร้างเป็น DataSet อันนี้ก็แล้วแต่ความถนัดของแต่ล่ะคน แต่บทความนี้จะใช้แบบ Wizard ส่วนการสร้างแบบ DataSet จะได้อ่านในบทความถัดไป

Screenshot

ReportViewer and Parameters

ตัวอย่าง Report ที่แสดงบนเว็บ (ReportViewer สามารถออก Report ได้ทั้งบน Web (Asp.net) หรือ Windows Application หรือบน Application อื่น ๆ )

Icons

ReportViewer and Parameters

Icons ของ Control ชื่อว่า MicrosoftReportViewer ที่อยู่บน Visual Studio 2005 (.NET 2.0) และ Visual Studio 2008 (.NET 3.0,3.5)


ReportViewer and Parameters

Icons ของ Control ชื่อว่า ReportViewer ที่อยู่บน Visual Studio 2010 (.NET 4.0)

Basic ReportViewer Control


เริ่มต้นการสร้าง Report

ตัวอย่างนี้จะเป็นการออก Report รายชื่อฐานข้อมูลของลูกค้า โดยมีตารางชื่อว่า customer จัดเก็บบนฐานข้อมูล SQL Server จาก Screenshot ข้างบนจะเห็นว่ามีการเปิดรับ Parameters ที่เป็น CustomerID และนำค่าที่ได้ไปค้นหาจากฐานข้อมูลและแสดงผลบน Report


Database ของ SQL Server
USE [mydatabase]
GO
/****** Object:  Table [dbo].[customer]    Script Date: 05/01/2012 16:44:29 ******/
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]

GO
SET ANSI_PADDING OFF

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);









นำ Query ไปรันเพื่อสร้าง Table และข้อมูล

ReportViewer and Parameters

โครงสร้างฐานข้อมูลที่ถูกจัดเก็บบนฐานข้อมูล SQL Server


ReportViewer and Parameters

สร้างโปรเจค ASP.NET Web Application หรือ Application อื่น ๆ เช่น Windows Form Application ซึ่งการใช้งานแทบจะไม่ต่างกันเลย

ReportViewer and Parameters

ไฟล์เริ่มต้นของ Project ตอนนี้ยังไม่มีไฟล์อะไรเพิ่มเติม นอกจากไฟล์ Default ที่มีมาให้กับการ New Project

ReportViewer and Parameters

ทำการ Add New Item เลือก DataSet เข้ามาใน Project ตั้งชื่อเป็น myDataSet.xsd

ReportViewer and Parameters

ไปยัง Server Explorer หรือ Database Explorer เพื่อ Browse ดูรายชื่อ Database ถ้าไม่มีให้ New ขึ้นมาใหม่

ReportViewer and Parameters

เปิดมุมมอง Design ของ myDataSet.xsd และลากตารางที่ต้องการออก Report (ดังรูป) ซึ่งจะได้เป็น DataTable และ TableAdapter ดังในรูป

เพิ่มเติม

ReportViewer and Parameters

กรณีที่ต้องการสร้างจาก Database หรือ Connection อื่น ๆ ก็สามารถสร้างได้เองโดยการคลิกขวาว่าง ๆ ของ DataSet เลือก Add -> Table Adapter

ReportViewer and Parameters

เลือก New Connection

ReportViewer and Parameters

เลือก DataSource ที่ต้องการ

ReportViewer and Parameters

รายชื่อ DataSource ต่าง ๆ ที่สามารถสร้าง Connection ได้

กลับมายังหน่าจอหลักของ DataSet

ReportViewer and Parameters

คลิกวาเลือก Add -> Query เพื่อจะสร้าง Parameters ที่จะรับค่าจากการส่งค่ามาจากภายนอก

ReportViewer and Parameters

เลือก Use SQL Statement ดังภาพ

ReportViewer and Parameters

เลือก SELECTwhich returns rows และคลิกที่ Next เพื่อไปขั้นตอนถัดไป

ReportViewer and Parameters

เขียน Query ที่อยู่ใน Datatable ที่ได้จากขั้นตอนการสร้าง DataSet จากตัวอย่างจะ WHERE CustomerID = @sCustomerID โดยที่ @sCustomerID คือค่าตัวแปร Parameters ที่รอรับค่าจากการส่งมาจาก DataSource สามารถทดสอบ Query ได้ที่ Query Builder...


ReportViewer and Parameters

กำหนดชื่อ Method Name ให้กำหนดเหมือนดังในภาพ

ReportViewer and Parameters

คลิกที่ Finish เพื่อเสร็จสิ้นขั้นตอนการสร้าง Query

ReportViewer and Parameters

หน้าจอ DataTable และ Parameters ที่ได้จากการสร้าง


กลับมาที่ Project หลัก

ReportViewer and Parameters

ให้ Add New Item เลือกเพื่อสร้าง Report ให้เลือก Report Wizard ดังภาพ โดยกำหนดชื่อเป็น myReport.rdlc


ReportViewer and Parameters

หน้าจอแสดงข้อความต้อนรับการสร้าง Report แบบ Wizard

ReportViewer and Parameters

เลือก DataSource ที่ได้จากขั้นตอนการสร้าง DataSet ซึ่งชื่อว่า customer

ReportViewer and Parameters

เลือกรูปแบบ Report

ReportViewer and Parameters

ปรับแต่ง Table ที่ได้จาก DataTable

ReportViewer and Parameters

ปรับแต่ง Layout ของ Report

ReportViewer and Parameters

เลือก Themes หรือ Style หรือสีของ Report

ReportViewer and Parameters

คลิกที่ Finish เพื่อเสร็จสิ้นการสร้าง Report แบบ Wizard

ReportViewer and Parameters

ตัวนี้เป็นไฟล์ myReport.rdlc ที่ได้จากการสร้าง Wizard ซึ่งสามารถปรับแต่ง Layout หรือขยายเพิ่มอื่น ๆ ได้ตามต้องการ








ขั้นตอนถัดไปคือจะเป็นการสร้าง DataSource เพื่อ Map ตัว Report กับ DataSet หรือ DataTable ที่จะแสดงผลในหน้า Web Page (.aspx)

ReportViewer and Parameters

กลับมาที่หน้า Web Form (.aspx) สร้าง Design ดังรูป โดยมี Label , Textbox ชื่อว่า txtCustomerID และ Button สำหรับคลิกเพื่อ Show Report และลาก Control ชื่อว่า MicrosoftReportViewer หรือ ReportViewer ใส่ใน Web Form ด้วย

ReportViewer and Parameters

สร้าง DataSource กรณีที่เราสร้างเป็น DataSet ไว้ สามารถสร้าง DataSource โดยการใช้ Control ที่ชื่อว่า ObjectDataSource ลากมาใส่ใน Form ดังรูป

ReportViewer and Parameters

เลือก DataSet และ DataTable ที่ได้จากขั้นตอนการสร้าง DataSet ดังภาพ

ReportViewer and Parameters

เลือก Method Name ที่ไดด้กำหนดจากขั้นตอนการสร้าง Query บน DataSet ซึ่งจะมีการกำหนด Parameters ชื่อว่า sCustomerID

ReportViewer and Parameters

จากขั้นตอนการเลือก Method name จากก่อนหน้านี้ Parameter ชื่อว่า sCustomerID เลือก Map กับ Control ของ Textbox ที่ชื่อว่า txtCustomerID ที่ได้สร้างบน Web Form


กลับมายัง Web Form

ReportViewer and Parameters

เลือกที่ ReportViewer Tasks เลือกไฟล์ Source Report ซึ่งชื่อว่า myReport.rdlc และกำหนด DataSource โดยการคลิกที่ Chooes Data Sources

ReportViewer and Parameters

Chooes Data Sources เลือก ObjectDataSource1 ซึ่งได้จากขั้นตอนการสร้าง ObjectDataSource ตอนนี้ได้ชื่อว่า ObjectDataSource1

เมื่อเสร็จสิ้นการกำหนด Report Source และ Data Source แล้วให้กลับมาที่ปุ่ม Button บน Web Form ให้สร้าง Event ดังนี้

- VB.NET

Imports System.Data.SqlClient
Imports Microsoft.Reporting.WebForms

Partial Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub btnShow_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnShow.Click
        ReportViewer1.LocalReport.Refresh()
    End Sub

End Class


- C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;
using Microsoft.Reporting.WebForms;

public partial class _Default : System.Web.UI.Page
{

	protected void btnShow_Click(object sender, EventArgs e)
	{
		ReportViewer1.LocalReport.Refresh();
	}

}


ทดสอบ Report

ReportViewer and Parameters

หน้าจอ Report ที่ได้จาก ReportViewer

ReportViewer and Parameters

ดูโครงสร้างไฟล์ซึ่งเพิ่มมา 2 ตัวคือ myDataSet.xsd กับ myReport.rdlc

สรุป
การใช้ ReportViewer เป็นอีกทางเลือกหนึ่งที่ต้องการออก Report บน Visual Studio แต่การใช้แบบ Wizard อาจจะมีความยุ่งยากและขั้นตอนเยอะ รวมทั้งข้อจำกัดอื่น ๆ แต่ถ้าต้องการสร้าง DataSet ผ่าน Code Behide แล้วค่อยโยน DataSource ให้กับ Report ก็เป็นวิธีหนึ่งที่น่าสนใจอีกวิธีหนึ่ง ซึ่งสามารถอ่านได้ที่บทความนี้

การสร้าง DataSet ให้กับ DataSources บน ReportViewer


ดาวน์โหลด Code ในบทความ
Download Code !!



บทความอื่น ๆ ที่เกี่ยวข้อง
Go to : ASP.NET ReportViewer - rsweb:ReportViewer
Go to : สุดยอดบทความ Crystal Report กับ .NET Framework (VB.NET , C#)
Go to : Walkthrough: Creating a ReportViewer Report


       
Bookmark.   
       

 

  By : TC Admin
  Score Rating : -
  Create Date : 2012-05-30 10:31:56
  Download : No files
     

Clound SSD Virtual Server
-->
Related Links
Crystal Report กับการสร้าง Report รายงานแบบ JOIN ข้อมูลหลายตาราง Table (.NET)
Crystal Report กับการสร้าง Report รายงานแบบ JOIN ข้อมูลหลายตาราง Table (.NET)
ตัวอย่างการออก report ของ crystal report ที่มีความซับซ้อนยิ่งขึ้น และการนำข้อมูลหลาย ๆ ตารางมาแสดง ใน report เดียวกัน
Rating :
Update :
2017-03-17 21:20:01 View : 65,510
.NET Windows Form กับการส่งอีเมล์ (Send Mail) แบบง่าย ๆ ด้วย System.Net.Mail (VB.NET , C#)
.NET Windows Form กับการส่งอีเมล์ (Send Mail) แบบง่าย ๆ ด้วย System.Net.Mail (VB.NET , C#)
ง่าย ๆ กับ .NET Framework การเขียน Windows Form ส่งอีเมล์ แบบง่าย ๆ ด้วย Namespace ของ System.Net.Mail
Rating :
Update :
2017-03-17 22:15:41 View : 18,258
สร้าง Crystal Report บน Visual Studio (VB.NET , C#) Step by Step
สร้าง Crystal Report บน Visual Studio (VB.NET , C#) Step by Step
บทความการสร้างรายงานแบบง่าย ๆ บน Crystal Report ซึ่งเป็น Report Tools ที่ได้รับความนิยมมากที่สุด ทำงานร่วมกับ VB.NET และ C#
Rating :
Update :
2017-03-24 21:25:06 View : 114,103
การสร้างฟอร์มด้วย DataGridView เบื้องต้นแบบ Step by Step [ VB.NET (Windows App+SQL Server) ]
การสร้างฟอร์มด้วย DataGridView เบื้องต้นแบบ Step by Step [ VB.NET (Windows App+SQL Server) ]
การสร้างฟอร์มด้วย DataGridView เบื้องต้นแบบ Step by Step Development Tool: Visual Studio 2010 Express Database Engine: MS SQL Server 2005 Express
Rating :
Update :
2011-07-24 19:33:14 View : 19,669
VB.NET/C# Login Check Username Password
VB.NET/C# Login Check Username Password
สร้างระบบล็อกอิน Login Form ตรวจสอบสิทธิ์ของ Username และ Password ก่อนจะเข้าสู่โปรแกรม
Rating :
Update :
2012-02-27 13:25:22 View : 26,154
ติดตั้ง Crystal Report ให้กับ Visual Studio 2012 (.NET 4.5) และเพิ่ม Items Control ลงใน Reporting
ติดตั้ง Crystal Report ให้กับ Visual Studio 2012 (.NET 4.5) และเพิ่ม Items Control ลงใน Reporting
ติดตั้ง crystal report ให้กับ visual studio 2012 ซึ่งปกติตอนที่ทำการ setup โปรแกรม vs 2012 จะไม่มีเป็น package มาให้ แต่จะต้องดาวน์โหลดเพื่อติดตั้งเพิ่มเติม
Rating :
Update :
2017-03-17 21:25:45 View : 51,356
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 02
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