Selasa, 30 April 2013

Aplikasi Database: Model Aplikasi CRUD

Aplikasi Database : Model Aplikasi CRUD
untuk Simpan, ubah , hapus , batal , - cari data .

untuk kode program silakan lihat dibawah

Buat databasenya dulu menggunakann microsoft access
dan kemudian bikin form seperti dibawah ini,


Klik untuk melihat Kode Program:
Kode Program:
Untuk membuat koneksi ke database:

Imports System.Data.OleDb
Module Module1
    Public Conn As OleDbConnection
    Public da As OleDbDataAdapter
    Public ds As DataSet
    Public cmd As OleDbCommand
    Public rd As OleDbDataReader
    Public Str As String
    Public Sub Koneksi()
        Str = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\JualBarang.accdb"
        Conn = New OleDbConnection(Str)
        If Conn.State = ConnectionState.Closed Then
            Conn.Open()
        End If
    End Sub

End Module

Kode Program untuk CRUD gambar seperti diatas;
Imports System.Data.OleDb
Public Class Form2
    Sub Kosong()
        TextBox1.Clear()
        TextBox2.Clear()
        TextBox1.Focus()
    End Sub
    Sub Isi()
        TextBox2.Clear()
        TextBox2.Focus()
    End Sub
    Sub TampilJenis()
        da = New OleDbDataAdapter("Select * From Jenis", Conn)
        ds = New DataSet
        ds.Clear()
        da.Fill(ds, "Jenis")
        DataGridView1.DataSource = ds.Tables("Jenis")
        DataGridView1.Refresh()
    End Sub
    Sub AturGrid()
        DataGridView1.Columns(0).Width = 60
        DataGridView1.Columns(1).Width = 200
        DataGridView1.Columns(0).HeaderText = "KODE JENIS"
        DataGridView1.Columns(1).HeaderText = "NAMA JENIS"
    End Sub



    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Call Koneksi()
        Call TampilJenis()
        Call Kosong()
        Call AturGrid()
    End Sub
    Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
        TextBox2.MaxLength = 50
        If e.KeyChar = Chr(13) Then
            TextBox2.Text = UCase(TextBox2.Text)
        End If
    End Sub
    Private Sub DataGridView1_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        Dim i As Integer
        i = Me.DataGridView1.CurrentRow.Index
        With DataGridView1.Rows.Item(i)
            Me.TextBox1.Text = .Cells(0).Value
            Me.TextBox2.Text = .Cells(1).Value
        End With
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text = "" Or TextBox2.Text = "" Then
            MsgBox("Data belum lengkap..!")
            TextBox1.Focus()
            Exit Sub
        Else
            cmd = New OleDbCommand("Select * From Jenis where KodeJenis='" & TextBox1.Text & "'", Conn)
            rd = cmd.ExecuteReader
            rd.Read()
            If Not rd.HasRows Then
                Dim Simpan As String = "insert into Jenis(KodeJenis,Jenis)values " & _
                "('" & TextBox1.Text & "','" & TextBox2.Text & "')"
                cmd = New OleDbCommand(Simpan, Conn)
                cmd.ExecuteNonQuery()
                MsgBox("Simpan data sukses...!", MsgBoxStyle.Information, "Perhatian")
            End If
            Call TampilJenis()
            Call Kosong()
            TextBox1.Focus()
        End If
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If TextBox1.Text = "" Then
            MsgBox("Kode Jenis belum diisi")
            TextBox1.Focus()
            Exit Sub
        Else
            Dim Ubah As String = "Update Jenis set " & _
            "Jenis='" & TextBox2.Text & "' " & _
            "where KodeJenis='" & TextBox1.Text & "'"
            cmd = New OleDbCommand(Ubah, Conn)
            cmd.ExecuteNonQuery()
            MsgBox("Ubah data sukses..!", MsgBoxStyle.Information, "Perhatian")
            Call TampilJenis()
            Call Kosong()
            TextBox1.Focus()
        End If
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If TextBox1.Text = "" Then
            MsgBox("Kode Barang belum diisi")
            TextBox1.Focus()
            Exit Sub
        Else
            If MessageBox.Show("Yakin akan menghapus Data Jenis " & TextBox1.Text & " ?", "", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                cmd = New OleDbCommand("Delete * From Jenis where KodeJenis='" & TextBox1.Text & "'", Conn)
                cmd.ExecuteNonQuery()
                Call Kosong()
                Call TampilJenis()
            Else
                Call Kosong()
            End If
        End If
    End Sub
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Call Kosong()
    End Sub
    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        TextBox1.MaxLength = 2
        If e.KeyChar = Chr(13) Then
            cmd = New OleDbCommand("Select * From Jenis where KodeJenis='" & TextBox1.Text & "'", Conn)
            rd = cmd.ExecuteReader
            rd.Read()
            If rd.HasRows = True Then
                TextBox2.Text = rd.Item(1)
                TextBox2.Focus()
            Else
                Call Isi()
                TextBox2.Focus()
            End If
        End If
    End Sub
    Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
        cmd = New OleDbCommand("Select * From Jenis where KodeJenis like '%" & TextBox3.Text & "%'", Conn)
        rd = cmd.ExecuteReader
        rd.Read()
        If rd.HasRows Then
            da = New OleDbDataAdapter("Select * From Jenis where KodeJenis like '%" & TextBox3.Text & "%'", Conn)
            ds = New DataSet
            da.Fill(ds, "Dapat")
            DataGridView1.DataSource = ds.Tables("Dapat")
            DataGridView1.ReadOnly = True
        Else
            MsgBox("Data tidak ditemukan")
        End If
    End Sub
End Class
==================

MODEL APLIKASI CRUD 
Buat database dan buat tampilan form seperti gambar dibawah ini ,
Jika sudah selesai masukkan kode programnya , maka akan jalan aplikasi CRUD nya ,,, 
gabar dibawah ini menunjukkan sedang mencari data yang telah dimasukkan kedalam database , terlihat seperti contoh dibawah ini


Untuk kode programnya ; lumayan banyak karena , ada tambahan fungsionalita untuk export data ke excel , print, -

Klik untuk melihat Kode Program:
Untuk Kode program buat  CRUD seperti gambar diatas , sekalian koneksi databasenya
Imports System.Data.OleDb
Imports System.Data.SqlClient

Public Class Form1
    Private dt As DataTable = Nothing
    Dim adapter As OleDbDataAdapter = Nothing
    Private Conn As OleDbConnection = Nothing
    Private cmd As OleDbCommand = Nothing
    Private sql As String = Nothing
    Private reader As OleDbDataReader = Nothing
    Private da As OleDbDataAdapter = Nothing
    Function Connect()
        If Not Conn Is Nothing Then
            Conn.Close()
        End If
        Conn.Open()
        Return Conn
    End Function
    Function Closedd()
        Conn.Close()
        Return Conn
    End Function

    Sub LoadData()
        Connect()
        sql = "select id_anggota from anggota"
        cmd = New OleDbCommand(sql, Conn)
        reader = cmd.ExecuteReader()
        Try
            CmbId.Items.Clear()
            While reader.Read
                CmbId.Items.Add(reader.GetString(0))
            End While
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        Closedd()
    End Sub

    Sub Tampil()
        Connect()
        Try
            Dim dt As New DataTable
            da = New OleDbDataAdapter("select * from anggota", Conn)
            da.Fill(dt)
            DataGridView1.DataSource = dt
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        Closedd()
    End Sub


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim sqlstr As String = "Select * from anggota"
        Dim connstr As String
        connstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\crud.accdb"
        Dim dc As DataColumn

        dt = New DataTable
        Try
            Conn = New OleDbConnection(connstr)
            Conn.Open()
            adapter = New OleDbDataAdapter(sqlstr, Conn)
            adapter.Fill(dt)

            'Memasukan nama kolom ke combobox
            For Each dc In dt.Columns
                CmbFilter.Items.Add(dc.ColumnName)
            Next
            CmbFilter.Items.RemoveAt(0)
            CmbFilter.SelectedIndex = 0
            Conn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            Conn.Dispose()

        End Try
        '====================
        If Not Conn Is Nothing Then Conn.Close()

        Dim ConnString As String

        ConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\crud.accdb"
        Try
            Conn = New OleDbConnection(ConnString)
            Conn.Open()
            '  Me.Text = "Mengubah data Anggota [Connected]"
            LoadData()
            Tampil()
            Conn.Close()
        Catch ex As Exception
            MessageBox.Show("Koneksi Error : " + ex.Message)
        End Try
        CmbKelamin.Items.Add("Pria")
        CmbKelamin.Items.Add("Wanita")
    End Sub

    Private Sub CmbId_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmbId.SelectedIndexChanged
        Connect()
        Dim Kunci As String = CmbId.Text
        sql = "select * from anggota where id_anggota='" & Kunci & "'"
        cmd = New OleDbCommand(sql, Conn)
        reader = cmd.ExecuteReader
        Try
            reader.Read()
            TxtNama.Text = reader.GetString(1)
            TxtAlamat.Text = reader.GetString(2)
            CmbKelamin.Text = reader.GetString(3)
            '===========================================
            TxtNamaz.Text = reader.GetString(4)
            TxtSocial.Text = reader.GetString(5)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        Closedd()
    End Sub

    Private Sub BtnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEdit.Click
        Connect()
        Dim hasil As Integer
        sql = "Update anggota set nama='" & TxtNama.Text & "',alamat='" & TxtAlamat.Text & "', kelamin='" & CmbKelamin.Text & "', juara='" & TxtNamaz.Text & "', social='" & TxtSocial.Text & "' where id_anggota='" & CmbId.Text & "'"
        cmd = New OleDbCommand(sql, Conn)
        Try
            hasil = cmd.ExecuteNonQuery
            If (hasil > 0) Then
                MessageBox.Show("Record Berhasil diubah", "Sukses", MessageBoxButtons.OK, MessageBoxIcon.Information)
                CmbId.Text = ""
                CmbKelamin.Text = ""

                TxtAlamat.Text = ""
                TxtNama.Text = ""
                TxtNamaz.Text = ""
                TxtSocial.Text = ""

                Tampil()
                LoadData()
            End If
        Catch ex As OleDbException
            MessageBox.Show("Failed : " & ex.Message)
        End Try
        Closedd()
    End Sub

    Private Sub BtnSimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSimpan.Click
        Connect()
        Dim insertquery As String
        Dim Kelamin As String
        Dim Hasil As Integer
        Dim cmd As OleDbCommand
        insertquery = ("insert into anggota(id_anggota,nama,alamat,juara,social,kelamin)Values('" & CmbId.Text & "','" & TxtNama.Text & "','" & TxtAlamat.Text & "','" & TxtNamaz.Text & "','" & TxtSocial.Text & "','" & Kelamin & "')")
        Try
            cmd = New OleDbCommand(insertquery, Conn)
            Hasil = cmd.ExecuteNonQuery
            If Hasil > 0 Then
                MessageBox.Show("record Berhasil Dimasukan", "Sukses", MessageBoxButtons.OK, MessageBoxIcon.Information)
                CmbId.Text = ""
                CmbKelamin.Text = ""

                TxtAlamat.Text = ""
                TxtNama.Text = ""
                TxtNamaz.Text = ""
                TxtSocial.Text = ""
                Tampil()
                LoadData()
            End If
        Catch ex As OleDbException
            MessageBox.Show("Failed : " & ex.Message, "Gagal", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        Closedd()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Connect()
        Dim index As Integer = CmbId.SelectedIndex
        Dim hasil As Integer
        Dim pesan As DialogResult
        sql = "delete from anggota where id_anggota='" & CmbId.Text & "'"
        pesan = MessageBox.Show("Yakin Menghapus Anngota " & Chr(10) & "dengan id" & CmbId.Text & "...?", "Konfirmasi", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
        cmd = New OleDbCommand(sql, Conn)
        Try
            If pesan = Windows.Forms.DialogResult.Yes = True Then
                hasil = cmd.ExecuteNonQuery
                CmbId.Text = ""
                CmbKelamin.Text = ""

                TxtAlamat.Text = ""
                TxtNama.Text = ""
                TxtNamaz.Text = ""
                TxtSocial.Text = ""
                Tampil()
            End If
        Catch ex As OleDbException
            MsgBox("Failed : " & ex.Message)
        End Try
        Closedd()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim dv As DataView
        Dim str As String
        Try
            dv = dt.DefaultView
            str = CmbFilter.Text & " Like '" & TxtKataKunci.Text & "%'"
            'Menyaring data record
            dv.RowFilter = str
            'Mengisi Data grid
            DataGridView1.DataSource = dv
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub


    Private Sub printDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
        Dim dataGridViewImage As New Bitmap(Me.DataGridView1.Width, Me.DataGridView1.Height)
        DataGridView1.DrawToBitmap(dataGridViewImage, New Rectangle(0, 0, Me.DataGridView1.Width, Me.DataGridView1.Height))
        e.Graphics.DrawImage(dataGridViewImage, 0, 0)
        '////////////////////////////////////
        '  e.Graphics.DrawString(TextBox1.Text, New Font("Verdana", 18, FontStyle.Bold), Brushes.Black, 10, 10)
        '  e.Graphics.DrawString(TextBox1.Text, TextBox1.Font, Brushes.Blue, 100, 100)
        'For i As Integer = 1 To 10
        '  e.Graphics.DrawString("Ejemplo de linea: " & i, New Font("Tahoma", 10, FontStyle.Bold), Brushes.Black, 10, 20 + (i * 30))
        ' Next
        '////////////////////////////////////
        ' e.Graphics.DrawString(Label1.Text, Label1.Font, Brushes.Black, 100, 100)
        '  e.Graphics.PageUnit = GraphicsUnit.Inch
    End Sub


    Private Sub GoToPrintToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GoToPrintToolStripMenuItem.Click
        PrintDocument1.Print()
    End Sub

    Private Sub TentukanPrinterToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TentukanPrinterToolStripMenuItem.Click
        PrintDialog1.Document = PrintDocument1 'PrintDialog associate with PrintDocument.
        If PrintDialog1.ShowDialog() = DialogResult.OK Then
            PrintDocument1.Print()
        End If
    End Sub

 

    Private Sub TentukanJenisPrintToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TentukanJenisPrintToolStripMenuItem.Click
        Dim dlg As New PrintDialog
        dlg.ShowDialog()
    End Sub

    Private Sub PrintPageSetupToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintPageSetupToolStripMenuItem.Click
        PageSetupDialog1.Document = PrintDocument1
        PageSetupDialog1.Document.DefaultPageSettings.Color = False
        PageSetupDialog1.ShowDialog()
    End Sub
    Private Sub PrintPreviewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintPreviewToolStripMenuItem.Click
        Call ono_initial_object(DataGridView1, "", "")
        AddHandler PrintDocument1.BeginPrint, AddressOf ono_PrintDocument_BeginPrint
        AddHandler PrintDocument1.PrintPage, AddressOf ono_PrintDocument_PrintPage
        PrintPreviewDialog1 = New PrintPreviewDialog
        With PrintPreviewDialog1
            PrintDocument1.PrinterSettings.DefaultPageSettings.Landscape = True
            .Document = PrintDocument1
            .ShowDialog()
        End With
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Call ono_initial_object(DataGridView1, "", "")
        AddHandler PrintDocument1.BeginPrint, AddressOf ono_PrintDocument_BeginPrint
        AddHandler PrintDocument1.PrintPage, AddressOf ono_PrintDocument_PrintPage
        PrintPreviewDialog1 = New PrintPreviewDialog
        With PrintPreviewDialog1
            PrintDocument1.PrinterSettings.DefaultPageSettings.Landscape = True
            .Document = PrintDocument1
            .ShowDialog()
        End With
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

        PageSetupDialog1.Document = PrintDocument1
        PageSetupDialog1.Document.DefaultPageSettings.Color = False
        PageSetupDialog1.ShowDialog()
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        PrintDocument1.Print()
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        Dim ExcelApp As Object, ExcelBook As Object
        Dim ExcelSheet As Object
        Dim i As Integer
        Dim j As Integer

        'create object of excel
        ExcelApp = CreateObject("Excel.Application")
        ExcelBook = ExcelApp.WorkBooks.Add
        ExcelSheet = ExcelBook.WorkSheets(1)

        With ExcelSheet
            For i = 1 To Me.DataGridView1.RowCount
                .cells(i, 1) = Me.DataGridView1.Rows(i - 1).Cells("id_anggota").Value
                For j = 1 To DataGridView1.Columns.Count - 1
                    .cells(i, j + 1) = DataGridView1.Rows(i - 1).Cells(j).Value
                Next
            Next
        End With

        ExcelApp.Visible = True
        '
        ExcelSheet = Nothing
        ExcelBook = Nothing
        ExcelApp = Nothing
    End Sub
End Class



Untuk Kode Program Fasilitas PRINT/ EXPORT/ maka buat module1.vb  // bikin module

Module Module1
    Private dgview As DataGridView = Nothing

    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 = "HEADER : Header pages"
    Private sUserName As String = "Printed By ....."


    Public Sub ono_initial_object(ByVal p_DataGridView As DataGridView, ByVal pTitleHeader As String, ByVal pUserDisplay As String)
        dgview = p_DataGridView
        Header = pTitleHeader
        sUserName = pUserDisplay
    End Sub

    Public Sub ono_PrintDocument_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs)
        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 dgview.Columns
            nTotalWidth += oColumn.Width
        Next
        nPageNo = 1
        NewPage = True
        nRowPos = 0
    End Sub

    Public Sub ono_PrintDocument_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)

        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

            For Each oColumn As DataGridViewColumn In dgview.Columns

                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

            Next

        End If

        Do While nRowPos < dgview.Rows.Count - 1

            Dim oRow As DataGridViewRow = dgview.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(dgview.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - e.Graphics.MeasureString(Header, New Font(dgview.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 13)

                    ' Draw Columns
                    nTop = e.MarginBounds.Top
                    i = 0
                    For Each oColumn As DataGridViewColumn In dgview.Columns

                        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

                    Next
                    NewPage = False

                End If

                nTop += nHeight
                i = 0
                For Each oCell As DataGridViewCell In oRow.Cells

                    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

                Next

            End If

            nRowPos += 1
            nRowsPerPage += 1

        Loop

        Call DrawFooter(e, nRowsPerPage)

        e.HasMorePages = False

    End Sub

    Private Sub DrawFooter(ByVal e As System.Drawing.Printing.PrintPageEventArgs, ByVal RowsPerPage As Int32)
        Dim sPageNo As String = nPageNo.ToString + " of " + Math.Ceiling(dgview.Rows.Count / RowsPerPage).ToString

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

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

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

    End Sub
End Module
==================


Silakan tinggalkan pesan, komentar, keluhan dan saran demi kemajuan isi blog: Form Komentar Munculan

Tidak ada komentar:

Posting Komentar

Catatan: Hanya anggota dari blog ini yang dapat mengirim komentar.