Programming for Everybody: Filter Crystal Report using combobox in VB.net with source code

Filter Crystal Report using combobox in VB.net with source code

 




Imports System.Data.SqlClient

Public Class Form1

    Dim conn As New SqlConnection("Data Source=.;Initial Catalog=sports;Integrated Security=true")

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        combo1()

        report1()

    End Sub

    Private Sub combo1()

        conn.Open()

        Dim strsql As New SqlCommand("Select Distinct sport From Table1", conn)

        Dim myreader As SqlDataReader = strsql.ExecuteReader

        ComboBox1.Items.Clear()

        While myreader.Read()

            ComboBox1.Items.Add(myreader("sport"))

        End While

        conn.Close()

    End Sub

    Private Sub report1()

        Dim cmd As New SqlCommand("Select * From Table1 Where sport Like '%" + ComboBox1.Text + "%'", conn)

        Dim da As New SqlDataAdapter(cmd)

        Dim dt As New DataTable

        da.Fill(dt)

        Dim cr_report As New CrystalReport1

        cr_report.SetDataSource(dt)

        CrystalReportViewer1.ReportSource = cr_report

        CrystalReportViewer1.Zoom(90%)

        CrystalReportViewer1.Refresh()

    End Sub


    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged

        report1()

    End Sub

End Class

No comments:

Post a Comment