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 > Forum > ปริ้นข้อมูลที่แสดงใน datagrid ทั้งหมดครับ winForm vb2008 sql server 2005 ครับ



 

ปริ้นข้อมูลที่แสดงใน datagrid ทั้งหมดครับ winForm vb2008 sql server 2005 ครับ

 



Topic : 083841



โพสกระทู้ ( 35 )
บทความ ( 0 )



สถานะออฟไลน์
Facebook



สั่งพิมพ์


=========================================================

จากรูปนะครับ คือผมมี datagird อยู่อันนึง มันจะแสดงข้อมูลตามที่ผมเลือกใน combobox ครับ คือผมอยากทำให้กดปุ่ม print แล้วมันเอาข้อมูลจาก datagrid ไปปริ้นครับ โดยที่จะเป็นข้อมูลจากใหนก็ได้ที่เราเลือกมาจาก combobox ครับ คือใน datagied มีข้อมูลอะไรอยู่ให้เอาข้อมูลนั้นไปปริ้นเลยอ่ะครับ
ผมต้องทำยังไงครับ

ขอบคุณล่วงหน้าครับ



Tag : .NET, Ms SQL Server 2005, VBScript, Crystal Report, VS 2008 (.NET 3.x), Windows







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2012-09-12 09:02:50 By : ultraman_ex View : 2886 Reply : 5
 

 

No. 1



โพสกระทู้ ( 219 )
บทความ ( 0 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

ที่ผมเคยทำก็คือ ดึงข้อมูลจาก Datagrid ไปเก็บเป็น DataTable
แล้วก็ เอา DataTable ไปทำเป็น DataSet เพื่อเอาไปออก report
ยังไงลองให้ท่านอื่นคอมเมนต์ดู อาจจะมีวิธีการที่ดีกว่านี้ครับ






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-09-12 09:51:29 By : mixarstudio
 


 

No. 2



โพสกระทู้ ( 26 )
บทความ ( 0 )



สถานะออฟไลน์
Twitter

ลองเทียบดูจาก link นี้ดูนะครับ น่าจะคล้าย ๆ กัน http://www.codeproject.com/Articles/28046/Printing-of-DataGridView
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-09-12 12:52:56 By : kittipongs
 

 

No. 3



โพสกระทู้ ( 35 )
บทความ ( 0 )



สถานะออฟไลน์
Facebook

ตอบความคิดเห็นที่ : 2 เขียนโดย : kittipongs เมื่อวันที่ 2012-09-12 12:52:56
รายละเอียดของการตอบ ::
ผมทำไม่เป็นอ่ะครับ มันเป็นของ vb หรือป่าวครับ ผมแปลงไม่เป็นอ่ะ รบกวนด้วยนะครับ^^

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-09-12 16:09:54 By : ultraman_ex
 


 

No. 4



โพสกระทู้ ( 26 )
บทความ ( 0 )



สถานะออฟไลน์
Twitter

จากที่ดูจากหน้าจอที่คุณ Design มาลองทำตามนี้นะครับ
1. Add Module เข้าไปตั้งชื่อ GridViewPrinter.vb
2. Code ใน GridViewPrinter.vb
Code (VB.NET)
Imports System.Drawing.Printing

Module GridViewPrinter

    Public DataGridViewToPrint As New DataGridView

    Public DefaultPageSettings As PageSettings = New PageSettings()

    Public WithEvents DocToPrint As New PrintDocument

    Private lPageNo As String = ""
    Private sPageNo As String = ""
    Private oStringFormat As StringFormat
    Private oStringFormatComboBox As StringFormat
    Private oButton As Button
    Private oCheckbox As CheckBox
    Private oComboBox As ComboBox
    Private nTotalWidth As Int16
    Private nRowPos As Int16
    Private NewPage As Boolean
    Private nPageNo As Int16
    Private Header As String
    Private FooterComment As String = ""

    Public Sub StartPrint(ByVal GridToPrint As DataGridView, ByVal PrintAsLandscape As Boolean, ByVal ShowPrintPreview As Boolean, ByVal HeaderToPrint As String, ByVal CommentToPrint As String)

        DataGridViewToPrint = GridToPrint
        Header = HeaderToPrint
        FooterComment = CommentToPrint

        'DataGridViewToPrint.Columns(2).Visible = False ' Use to hide a col. (index no.)

        ' Set up Default Page Settings
        DocToPrint.DefaultPageSettings.Landscape = PrintAsLandscape

        DocToPrint.DefaultPageSettings.Margins.Left = 25
        DocToPrint.DefaultPageSettings.Margins.Right = 75
        DocToPrint.DefaultPageSettings.Margins.Top = 25
        DocToPrint.DefaultPageSettings.Margins.Bottom = 75

        DocToPrint.OriginAtMargins = True ' takes margins into account 

        If ShowPrintPreview = True Then

            Dim dlgPrintPreview As New FormPrintPreviewDialog

            dlgPrintPreview.ClientSize = New System.Drawing.Size(800, 600)
            dlgPrintPreview.Document = DocToPrint ' Previews print
            dlgPrintPreview.ShowDialog()

        Else

            '  Allow the user to choose a printer and specify other settings.
            Dim dlgPrint As New PrintDialog

            With dlgPrint
                .AllowSelection = False
                .ShowNetwork = False
                .AllowCurrentPage = True
                .AllowSomePages = True
                .Document = DocToPrint
            End With

            '  If the user clicked OK, print the document.
            If dlgPrint.ShowDialog = Windows.Forms.DialogResult.OK Then
                DocToPrint.Print()
            End If

        End If

    End Sub

    Public Sub DocToPrint_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles DocToPrint.BeginPrint

        oStringFormat = New StringFormat
        oStringFormat.Alignment = StringAlignment.Near
        oStringFormat.LineAlignment = StringAlignment.Center
        oStringFormat.Trimming = StringTrimming.EllipsisCharacter

        oStringFormatComboBox = New StringFormat
        oStringFormatComboBox.LineAlignment = StringAlignment.Center
        oStringFormatComboBox.FormatFlags = StringFormatFlags.NoWrap
        oStringFormatComboBox.Trimming = StringTrimming.EllipsisCharacter

        oButton = New Button
        oCheckbox = New CheckBox
        oComboBox = New ComboBox

        nTotalWidth = 0

        For Each oColumn As DataGridViewColumn In DataGridViewToPrint.Columns
            If oColumn.Visible = True Then ' Prints only Visible columns
                nTotalWidth += oColumn.Width
            End If
        Next

        nPageNo = 1
        NewPage = True
        nRowPos = 0

    End Sub

    Public Sub DocToPrint_EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles DocToPrint.EndPrint
        'Not currently used
    End Sub

    Public Sub DocToPrint_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles DocToPrint.PrintPage

        Static oColumnLefts As New ArrayList
        Static oColumnWidths As New ArrayList
        Static oColumnTypes As New ArrayList
        Static nHeight As Int16

        Dim nWidth, i, nRowsPerPage As Int16
        Dim nTop As Int16 = e.MarginBounds.Top
        Dim nLeft As Int16 = e.MarginBounds.Left

        If nPageNo = 1 Then

            oColumnLefts.Clear()
            oColumnWidths.Clear()
            oColumnTypes.Clear()

            For Each oColumn As DataGridViewColumn In DataGridViewToPrint.Columns
                If oColumn.Visible = True Then
                    nWidth = CType(Math.Floor(oColumn.Width / nTotalWidth * nTotalWidth * (e.MarginBounds.Width / nTotalWidth)), Int16)

                    nHeight = e.Graphics.MeasureString(oColumn.HeaderText, oColumn.InheritedStyle.Font, nWidth).Height + 11

                    oColumnLefts.Add(nLeft)
                    oColumnWidths.Add(nWidth)
                    oColumnTypes.Add(oColumn.GetType)
                    nLeft += nWidth
                End If
            Next

        End If

        Do While nRowPos < DataGridViewToPrint.Rows.Count - 1

            Dim oRow As DataGridViewRow = DataGridViewToPrint.Rows(nRowPos)

            If nTop + nHeight >= e.MarginBounds.Height + e.MarginBounds.Top Then

                DrawFooter(e, nRowsPerPage)

                NewPage = True
                nPageNo += 1
                e.HasMorePages = True
                Exit Sub

            Else

                If NewPage Then

                    ' Draw Header
                    e.Graphics.DrawString(Header, New Font(DataGridViewToPrint.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - e.Graphics.MeasureString(Header, New Font(DataGridViewToPrint.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 13)

                    ' Draw Columns
                    nTop = e.MarginBounds.Top
                    i = 0
                    For Each oColumn As DataGridViewColumn In DataGridViewToPrint.Columns
                        If oColumn.Visible = True Then
                            e.Graphics.FillRectangle(New SolidBrush(Drawing.Color.LightGray), New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight))
                            e.Graphics.DrawRectangle(Pens.Black, New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight))
                            e.Graphics.DrawString(oColumn.HeaderText, oColumn.InheritedStyle.Font, New SolidBrush(oColumn.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i), nTop, oColumnWidths(i), nHeight), oStringFormat)
                            i += 1
                        End If

                    Next
                    NewPage = False

                End If

                nTop += nHeight
                i = 0
                For Each oCell As DataGridViewCell In oRow.Cells
                    If oCell.Visible = True Then
                        If oColumnTypes(i) Is GetType(DataGridViewTextBoxColumn) OrElse oColumnTypes(i) Is GetType(DataGridViewLinkColumn) Then

                            e.Graphics.DrawString(oCell.Value.ToString, oCell.InheritedStyle.Font, New SolidBrush(oCell.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i), nTop, oColumnWidths(i), nHeight), oStringFormat)

                        ElseIf oColumnTypes(i) Is GetType(DataGridViewButtonColumn) Then

                            oButton.Text = oCell.Value.ToString
                            oButton.Size = New Size(oColumnWidths(i), nHeight)
                            Dim oBitmap As New Bitmap(oButton.Width, oButton.Height)
                            oButton.DrawToBitmap(oBitmap, New Rectangle(0, 0, oBitmap.Width, oBitmap.Height))
                            e.Graphics.DrawImage(oBitmap, New Point(oColumnLefts(i), nTop))

                        ElseIf oColumnTypes(i) Is GetType(DataGridViewCheckBoxColumn) Then

                            oCheckbox.Size = New Size(14, 14)
                            oCheckbox.Checked = CType(oCell.Value, Boolean)
                            Dim oBitmap As New Bitmap(oColumnWidths(i), nHeight)
                            Dim oTempGraphics As Graphics = Graphics.FromImage(oBitmap)
                            oTempGraphics.FillRectangle(Brushes.White, New Rectangle(0, 0, oBitmap.Width, oBitmap.Height))
                            oCheckbox.DrawToBitmap(oBitmap, New Rectangle(CType((oBitmap.Width - oCheckbox.Width) / 2, Int32), CType((oBitmap.Height - oCheckbox.Height) / 2, Int32), oCheckbox.Width, oCheckbox.Height))
                            e.Graphics.DrawImage(oBitmap, New Point(oColumnLefts(i), nTop))

                        ElseIf oColumnTypes(i) Is GetType(DataGridViewComboBoxColumn) Then

                            oComboBox.Size = New Size(oColumnWidths(i), nHeight)
                            Dim oBitmap As New Bitmap(oComboBox.Width, oComboBox.Height)
                            oComboBox.DrawToBitmap(oBitmap, New Rectangle(0, 0, oBitmap.Width, oBitmap.Height))
                            e.Graphics.DrawImage(oBitmap, New Point(oColumnLefts(i), nTop))
                            e.Graphics.DrawString(oCell.Value.ToString, oCell.InheritedStyle.Font, New SolidBrush(oCell.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i) + 1, nTop, oColumnWidths(i) - 16, nHeight), oStringFormatComboBox)

                        ElseIf oColumnTypes(i) Is GetType(DataGridViewImageColumn) Then

                            Dim oCellSize As Rectangle = New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight)
                            Dim oImageSize As Size = CType(oCell.Value, Image).Size
                            e.Graphics.DrawImage(oCell.Value, New Rectangle(oColumnLefts(i) + CType(((oCellSize.Width - oImageSize.Width) / 2), Int32), nTop + CType(((oCellSize.Height - oImageSize.Height) / 2), Int32), CType(oCell.Value, Image).Width, CType(oCell.Value, Image).Height))

                        End If

                        e.Graphics.DrawRectangle(Pens.Black, New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight))

                        i += 1
                    End If
                Next

            End If

            nRowPos += 1
            nRowsPerPage += 1

        Loop

        DrawFooter(e, nRowsPerPage)

        e.HasMorePages = False

    End Sub

    Public Sub DrawFooter(ByVal e As System.Drawing.Printing.PrintPageEventArgs, ByVal RowsPerPage As Int32)

        Dim sPageNo As String = nPageNo.ToString + " of "

        If nPageNo = "1" Then

            lPageNo = Math.Ceiling((DataGridViewToPrint.Rows.Count - 1) / RowsPerPage).ToString()
            sPageNo = nPageNo.ToString + " of " + lPageNo

        Else

            sPageNo = nPageNo.ToString + " of " + lPageNo

        End If

        ' Right Align - User Name
        e.Graphics.DrawString(FooterComment, DataGridViewToPrint.Font, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(FooterComment, DataGridViewToPrint.Font, e.MarginBounds.Width).Width), e.MarginBounds.Top + e.MarginBounds.Height + 7)

        ' Left Align - Date/Time
        e.Graphics.DrawString(Now.ToLongDateString + " " + Now.ToShortTimeString, DataGridViewToPrint.Font, Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top + e.MarginBounds.Height + 7)

        ' Center - Page No. Info
        e.Graphics.DrawString(sPageNo, DataGridViewToPrint.Font, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(sPageNo, DataGridViewToPrint.Font, e.MarginBounds.Width).Width) / 2, e.MarginBounds.Top + e.MarginBounds.Height + 7)

    End Sub

End Module

3.Add Form ตั้งชื่อ FormPrintPreviewDialog
4.เปิด FormPrintPreviewDialog.Designer.vb
5.แก้ไข Inherits System.Windows.Forms.Form เป็น Inherits System.Windows.Forms.PrintPreviewDialog
6.comment line ดังนี้
Code (VB.NET)
    '<System.Diagnostics.DebuggerStepThrough()> _
    'Private Sub InitializeComponent()
    '    components = New System.ComponentModel.Container
    '    Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
    '    Me.Text = "Form3"
    'End Sub

7. วาง code เพิ่มใน FormPrintPreviewDialog.Designer.vb
Code (VB.NET)
Dim ShowCounter As Boolean = True

    Private Sub myPrintPreview_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown

        If ShowCounter = True Then

            'Get the toolstrip from the base control
            Dim ts As ToolStrip = CType(Me.Controls(1), ToolStrip)

            'Get the print button from the toolstrip
            Dim printItem As ToolStripItem = ts.Items("printToolStripButton")

            'Add a new button (our own) to the toolstrip by copying properties from the orginal
            With printItem

                Dim myPrintItem As ToolStripItem
                myPrintItem = ts.Items.Add(.Text, .Image, New EventHandler(AddressOf MyPrintItemClicked))

                'Alter as many other personalized stuff here
                myPrintItem.DisplayStyle = ToolStripItemDisplayStyle.Image

                'Relocate the item to the beginning of the toolstrip
                ts.Items.Insert(0, myPrintItem)

            End With

            'Remove the orginal button
            ts.Items.Remove(printItem)

            ShowCounter = False

        End If

    End Sub

    Private Sub MyPrintItemClicked(ByVal sender As Object, ByVal e As EventArgs)

        'Allow the user to choose a printer and specify other settings.
        Dim dlgPrint As New PrintDialog

        With dlgPrint
            .Document = Me.Document
            '.UseEXDialog = True
            .AllowSelection = False
            .ShowNetwork = False
            .AllowCurrentPage = True
            .AllowSomePages = True
        End With

        'If the user clicked OK, print the document.
        If dlgPrint.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.Document.Print()
        End If

    End Sub

    Private Sub InitializeComponent()
        Me.SuspendLayout()
        '
        '
        Me.ClientSize = New System.Drawing.Size(703, 300)
        Me.Name = "FormPrintPreviewDialog"
        Me.ResumeLayout(False)

    End Sub

    Dim ShowCounter As Boolean = True

    Private Sub myPrintPreview_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown

        If ShowCounter = True Then

            'Get the toolstrip from the base control
            Dim ts As ToolStrip = CType(Me.Controls(1), ToolStrip)

            'Get the print button from the toolstrip
            Dim printItem As ToolStripItem = ts.Items("printToolStripButton")

            'Add a new button (our own) to the toolstrip by copying properties from the orginal
            With printItem

                Dim myPrintItem As ToolStripItem
                myPrintItem = ts.Items.Add(.Text, .Image, New EventHandler(AddressOf MyPrintItemClicked))

                'Alter as many other personalized stuff here
                myPrintItem.DisplayStyle = ToolStripItemDisplayStyle.Image

                'Relocate the item to the beginning of the toolstrip
                ts.Items.Insert(0, myPrintItem)

            End With

            'Remove the orginal button
            ts.Items.Remove(printItem)

            ShowCounter = False

        End If

    End Sub

    Private Sub MyPrintItemClicked(ByVal sender As Object, ByVal e As EventArgs)

        'Allow the user to choose a printer and specify other settings.
        Dim dlgPrint As New PrintDialog

        With dlgPrint
            .Document = Me.Document
            '.UseEXDialog = True
            .AllowSelection = False
            .ShowNetwork = False
            .AllowCurrentPage = True
            .AllowSomePages = True
        End With

        'If the user clicked OK, print the document.
        If dlgPrint.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.Document.Print()
        End If

    End Sub

    Private Sub InitializeComponent()
        Me.SuspendLayout()
        '
       
        '
        Me.ClientSize = New System.Drawing.Size(703, 300)
        Me.Name = "FormPrintPreviewDialog"
        Me.ResumeLayout(False)

    End Sub

8. ตรงปุ่ม Print หน้าจอคุณ เพิ่ม event code ดังนี้
Code (VB.NET)
GridViewPrinter.StartPrint(DataGridView1, False, True, "Title Here", "Comment  Here")


ลองเพิ่มเติมดูนะครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-09-13 14:58:45 By : kittipongs
 


 

No. 5



โพสกระทู้ ( 35 )
บทความ ( 0 )



สถานะออฟไลน์
Facebook

ตอบความคิดเห็นที่ : 4 เขียนโดย : kittipongs เมื่อวันที่ 2012-09-13 14:58:45
รายละเอียดของการตอบ ::
ขอบคุณมากๆเลยครับ^^ เดี๋ยวกมลองทำดูครับ

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-09-15 00:56:31 By : ultraman_ex
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ปริ้นข้อมูลที่แสดงใน datagrid ทั้งหมดครับ winForm vb2008 sql server 2005 ครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 04
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 อัตราราคา คลิกที่นี่