Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Implementing Multiple Criteria Filtration of Data in Data Grid in .NET 2.0

Author: DevASP
Download Source Code : 595_Filtration.zip

In this simple article you will learn that how you can implement Multiple Criteria Filtration of Data in Data Grid

Simply just create New Project of console application in VB.

 

Drag one text box, one Combo box, two numeric up down, two date time Picker and a Data Grid on form.

 

In this sample I am using MS Access database and getting values from access on run time and after that apply different filtration criteria. This is a good example for beginners.

 

Import this library

 

 

Imports ss = System.Data.SqlClient  

 Now declare global variables

 

  Dim cn As New System.Data.OleDb.OleDbConnection

  Dim cm As New System.Data.OleDb.OleDbCommand

  Dim rd As System.Data.OleDb.OleDbDataReader

 Now just write code against these events

  

 

Private Sub btnfilter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfilter.Click

        cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=abc.mdb"

        Try

            cn.Open()

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try

        cm.Connection = cn

 

        cm.CommandText = "select * from pDetails where price >= " & NUD1.Value & " and price <=" & NUD2.Value & " and  Name like '" & txtbyName.Text & "%' and (pdetails.Date) >=#" & dtpDetails.Text & "# and (pdetails.Date) <=#" & dtp2.Value & "#"

 

        Try

            Dim adp As New Data.OleDb.OleDbDataAdapter

            Dim ds As New DataSet

            Dim dv As New DataView

            adp.SelectCommand = cm

            adp.Fill(ds, "dataset")

            dv.Table = ds.Tables(0)

            dv.Sort = "Name"

            DataGrid1.DataSource = dv

            DataGrid1.Refresh()

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        Finally

            cn.Close()

        End Try

    End Sub

    Private Sub txtbyName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtbyName.TextChanged

        cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=abc.mdb"

        Try

            cn.Open()

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try

        cmbbyName.Items.Clear()

        cm.Connection = cn

        cm.CommandText = "select * from pDetails where Name like '" & txtbyName.Text & "%'"

        rd = cm.ExecuteReader()

 

        cmbbyName.Items.Clear()

        While (rd.Read)

            cmbbyName.Items.Add(rd("Name"))

        End While

        rd.Close()

        If cmbbyName.Items.Count > 0 Then

            cmbbyName.SelectedIndex = 0

        End If

        Try

            Dim adp As New Data.OleDb.OleDbDataAdapter

            Dim ds As New DataSet

            Dim dv As New DataView

            adp.SelectCommand = cm

            adp.Fill(ds, "dataset")

            dv.Table = ds.Tables(0)

            dv.Sort = "name"

            DataGrid1.DataSource = dv

            DataGrid1.Refresh()

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        Finally

            cn.Close()

        End Try

    End Sub

 

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

        cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=abc.mdb"

        Try

            cn.Open()

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try

        cmbbyName.Items.Clear()

        cm.Connection = cn

        cm.CommandText = "select * from pDetails where Name like '" & txtbyName.Text & "%'"

        rd = cm.ExecuteReader()

 

        cmbbyName.Items.Clear()

        While (rd.Read)

            cmbbyName.Items.Add(rd("Name"))

        End While

        rd.Close()

        If cmbbyName.Items.Count > 0 Then

            cmbbyName.SelectedIndex = 0

        End If

        Try

            Dim adp As New Data.OleDb.OleDbDataAdapter

            Dim ds As New DataSet

            Dim dv As New DataView

            adp.SelectCommand = cm

            adp.Fill(ds, "dataset")

            dv.Table = ds.Tables(0)

            dv.Sort = "name"

            DataGrid1.DataSource = dv

            DataGrid1.Refresh()

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        Finally

            cn.Close()

        End Try

 

    End Sub

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to Populate values in Data Grid and Combo Box from database on Run time depend upon Given Text .NET 2.0.

Disclaimer - Privacy
© 2002-2017 DevASP.net