VB.Net Programming - Adding a ReportViewer Report to VB.Net Project Using RDLC File


Visual Basic .Net Programming - How to Add a ReportViewer Report to VB.Net Project Using RDLC File.

🎓 Master Visual Basic .NET and Access Database By Building the Point Of Sale System (POS).
ðŸ“ē Enroll Now: https://bit.ly/2WcbRhX

[Screenshot]


Part 1/2


[Source Code Part 1]

'- Form: Form1

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'Your_DatabaseDataSet.Table1' table. You can move, or remove it, as needed.
        Me.Table1TableAdapter.Fill(Me.Your_DatabaseDataSet.Table1)

        With DataGridView1
            .ClearSelection()
            .ReadOnly = True
            .MultiSelect = False

            '- Hide ID Column
            .Columns(0).Visible = False

            '- Change the order of columns in DataGridView1 Control
            .Columns(1).DisplayIndex = 0
            .Columns(2).DisplayIndex = 1
            .Columns(3).DisplayIndex = 3
            .Columns(4).DisplayIndex = 4
            .Columns(5).DisplayIndex = 5
            .Columns(6).DisplayIndex = 2
            .Columns(7).DisplayIndex = 7

            '- Change the text in the Column Header
            .Columns(1).HeaderText = "First Name"
            .Columns(2).HeaderText = "Last Name"

            .Columns(4).Width = 170 '- Email
            .Columns(5).Width = 115 '- Facebook

        End With

        Dim colors() As String = [Enum].GetNames(GetType(KnownColor))

        cboDfColor.Items.AddRange(colors)
        cboAltColor.Items.AddRange(colors)

        Me.reset()

    End Sub

    Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        Table1BindingSource.MovePrevious()
    End Sub

    Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
        Table1BindingSource.AddNew()
    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        Table1BindingSource.MoveNext()
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        On Error GoTo SaveErr
        Table1BindingSource.EndEdit()
        Table1TableAdapter.Update(Your_DatabaseDataSet.Table1)

ErrEx:
        Exit Sub
SaveErr:
        MsgBox("Error Number " & Err.Number & vbNewLine & _
               "Error Description " & Err.Description, MsgBoxStyle.Critical, _
               "Reset Error!")
        Resume ErrEx

    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

        If MessageBox.Show("Are you sure", "Delete Record", MessageBoxButtons.YesNo, _
                           MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = _
                       Windows.Forms.DialogResult.Yes Then
            Table1BindingSource.RemoveCurrent()
        End If

    End Sub

    Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click

        Dim msg As String = "Do you want to Exit?"
        Dim title As String = "Exit Application"

        If MessageBox.Show(msg, title, MessageBoxButtons.YesNo, _
                           MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = _
                       Windows.Forms.DialogResult.Yes Then
            Me.Close() 'End
        End If

    End Sub
 
    Private Sub cboDfColor_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboDfColor.SelectedIndexChanged
        cboDfColor.BackColor = Color.FromName(cboDfColor.SelectedItem)
        txtSearch.Select()
    End Sub

    Private Sub cboAltColor_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboAltColor.SelectedIndexChanged
        cboAltColor.BackColor = Color.FromName(cboAltColor.SelectedItem)
        txtSearch.Select()
    End Sub

    Private Sub lblReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblReset.Click
        On Error GoTo ErrRe
        txtSearch.Select()
        Table1BindingSource.Filter = Nothing

        With DataGridView1
            .ClearSelection()
            .ReadOnly = True
            .MultiSelect = False
            .DataSource = Table1BindingSource
        End With

        Me.reset()

ErrEx:
        Exit Sub
ErrRe:
        MsgBox("Error Number " & Err.Number & vbNewLine & _
               "Error Description " & Err.Description, MsgBoxStyle.Critical, _
               "Reset Error!")
        Resume ErrEx
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        On Error GoTo SearchErr

        If txtSearch.Text = "" Then
            Call notFound()
            Exit Sub

        Else

            Dim cantFind As String = txtSearch.Text
            Me.dgvFill()

            Table1BindingSource.Filter = "(Convert(ID, 'System.String') LIKE '" & txtSearch.Text & "')" &
                "OR (YourName LIKE '" & txtSearch.Text & "') OR (Lastname LIKE '" & txtSearch.Text & "')" &
                "OR (Phone LIKE '" & txtSearch.Text & "') OR (Email LIKE '" & txtSearch.Text & "')" &
                "OR (Facebook LIKE '" & txtSearch.Text & "') OR ([Job Title] LIKE '" & txtSearch.Text & "')"

            If Table1BindingSource.Count <> 0 Then
                With DataGridView1
                    .DataSource = Table1BindingSource
                End With

            Else

                Me.notFound()

                MsgBox("--> " & cantFind & vbNewLine & _
                       "The search item was not found.", _
                       MsgBoxStyle.Information, "Hey Boss!")

                Table1BindingSource.Filter = Nothing

                With DataGridView1
                    .ClearSelection()
                    .ReadOnly = True
                    .MultiSelect = False
                    .DataSource = Table1BindingSource
                End With

            End If

        End If

ErrExit:
        Exit Sub
SearchErr:
        MsgBox("Error Number " & Err.Number & vbNewLine & _
               "Error Description " & Err.Description, MsgBoxStyle.Critical, _
               "Reset Error!")
        Resume ErrExit

    End Sub

    Private Sub reset()

        Dim txtS As TextBox = txtSearch
        With txtS
            .Text = ""
            .Select()
            .BackColor = Color.LightCyan
        End With

        cboDfColor.SelectedItem = "MistyRose"
        cboAltColor.SelectedItem = "Gold"

        cboDfColor.BackColor = Color.LightCyan
        cboAltColor.BackColor = Color.LightCyan

        If DataGridView1.DataSource Is Nothing Then
            Exit Sub
        End If

        Dim dgv1 As DataGridView = DataGridView1
        With dgv1
            .RowsDefaultCellStyle.BackColor = Color.FromName(cboDfColor.SelectedItem)
            .AlternatingRowsDefaultCellStyle.BackColor = Color.FromName(cboAltColor.SelectedItem)
        End With

    End Sub

    Private Sub dgvFill()

        txtSearch.BackColor = Color.LightBlue

        If DataGridView1.DataSource Is Nothing Then
            Exit Sub
        End If

        Dim myDGV1 As DataGridView = DataGridView1
        With myDGV1
            .RowsDefaultCellStyle.BackColor = Color.FromName(cboDfColor.SelectedItem)
            .AlternatingRowsDefaultCellStyle.BackColor = Color.FromName(cboAltColor.SelectedItem)
        End With

    End Sub

    Private Sub notFound()

        Dim txtS As TextBox = txtSearch
        With txtS
            .BackColor = Color.White
            .Select()
            .SelectAll()
        End With

        If DataGridView1.DataSource Is Nothing Then
            Exit Sub
        End If

        Dim dgv As DataGridView = DataGridView1
        With dgv
            .RowsDefaultCellStyle.BackColor = Color.White
            .AlternatingRowsDefaultCellStyle.BackColor = Color.White
        End With
    End Sub

    Private Sub btnOpenReport_Click(sender As Object, e As EventArgs) Handles btnOpenReport.Click
        Dim f As New formEmployeeReport()
        f.TopMost = True
        f.Show()
    End Sub

End Class

[Screenshot]

[Part 2/2]

[Source Code Part 2]

'- Form: formEmployeeReport

Public Class formEmployeeReport
    Private Sub formEmployeeReport_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'Employee_Dataset.Table1' table. You can move, or remove it, as needed.
        Me.Table1TableAdapter.Fill(Me.Employee_Dataset.Table1)

        Me.ReportViewer1.RefreshReport()
    End Sub

    Private Sub ButtonSearch_Click(sender As Object, e As EventArgs) Handles ButtonSearch.Click
        Try
            If Not String.IsNullOrEmpty(TextBoxSearch.Text) Then
                Dim keyword As String = Me.TextBoxSearch.Text.Trim()
                Me.Table1BindingSource.Filter =
                    "(YourName LIKE '%" & keyword & "%')" &
                    "OR (LastName LIKE '%" & keyword & "%')" &
                    "OR (Phone LIKE '%" & keyword & "%')" &
                    "OR (Email LIKE '%" & keyword & "%')" &
                    "OR (Facebook LIKE '%" & keyword & "%')" &
                    "OR (JobTitle LIKE '%" & keyword & "%')" 'No Space
                Me.ReportViewer1.RefreshReport()
            Else
                Me.Table1BindingSource.RemoveFilter()
                formEmployeeReport_Load(sender, e)
            End If
        Catch ex As Exception
            MessageBox.Show("Error: " + ex.Message.ToString(),
                            "Error Handler - Code A Minute.",
                            MessageBoxButtons.OK, MessageBoxIcon.Information)
        Finally
            Me.TextBoxSearch.Focus()
        End Try
    End Sub

    Private Sub TextBoxSearch_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBoxSearch.KeyDown
        If (e.KeyCode = Keys.Enter) Then
            ButtonSearch.PerformClick()
        End If
    End Sub
End Class

★ Related Contents ★
» Visual Basic .Net : Search in Access Database - DataGridView BindingSource Filter.
✩ Part 1
» https://youtu.be/UoT2oava9ns
✩ Part 2
» https://youtu.be/e5Dvkw7moWg
.
See Also:
» Programming in Visual Basic .Net How to Connect Access Database to VB .Net
» https://youtu.be/cwDqjmSmtMQ
.
[Source Code]
» https://codeaminute.blogspot.com/2014/07/access-database-search-form.html?m=1

My Twitter

My Facebook

My Google+

My YouTube Channel

Thank you very much.
āļ‚āļ­āļšāļ„ุāļ“āļ„āļĢัāļš.

Comments