Search Filter DataGridView using InputBox dialog in Windows Application

Last Reply 5 months ago By dharmendr

Posted 5 months ago

Hi Everyone, 

I have this code that I use for search in my DataGridView, it work with ID column but not with Date and Description columns, I think it doesn’t show multiple results but only one but I am not sure. 

when I type into the search button the ID it shows me but when i type description or Date

Here is the code :

    Private Sub GroceryDataGridView_ColumnHeaderMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles GroceryDataGridView.ColumnHeaderMouseDoubleClick
        Dim sqlstr As String
        Dim ds As New DataSet
        Dim SearchData As String
        SearchData = InputBox("Search Date", "Enter information ")
        If SearchData = "" Then
            Exit Sub
        End If
        Select Case e.ColumnIndex
            Case 1
                sqlstr = "ID=" & SearchData

            Case 2
                sqlstr = "Description=" & SearchData

            Case Else
                sqlstr = "Date=" & SearchData
        End Select
        ds = GetData("Expenses", sqlstr)
        GroceryDataGridView.DataSource = ds.Tables(0)
    End Sub

    Public Function GetData(ByVal Tablename As String, ByVal FilterString As String) As DataSet
        On Error Resume Next
        Dim cmd As New OleDb.OleDbCommand
        Dim ids As New DataSet

        GetData = Nothing
        m_dbcon.ConnectionString = "Provider = sqloledb;Data Source=;Initial Catalog=Expenses;Trusted_Connection=yes;"
        m_dbcon.Open()

        cmd.Connection = m_dbcon
        If FilterString.Contains("Where") Then
        Else
            If FilterString <> "" Then FilterString = "WHERE " & FilterString
        End If

        If (FilterString = "") Then
            cmd.CommandText = String.Format("Select ID, Date , Description, Quantity, Price, Store, Payment, MwSt from {0}", Tablename)

        Else
            cmd.CommandText = String.Format("Select ID, Date , Description, Quantity, Price, Store, Payment, MwSt from {0} {1} ", Tablename, FilterString)
        End If

        m_DataAdapter.SelectCommand = cmd
        m_DataAdapter.Fill(ids, Tablename)

        GetData = ids
        m_dbcon.Close()
        Return GetData
    End Function

    Private Sub releaseObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
    End Sub

Thank you for your help, Best Regards

This question does not have replies that have been liked.