Results 1 to 10 of 10

Thread: [RESOLVED] Filter .DBF File In DataGridView

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2015
    Posts
    16

    Resolved [RESOLVED] Filter .DBF File In DataGridView

    Hello,

    I'm trying to filter a search on a .DBF file and I'm running into some errors. From what I can gather the error I keep getting is...

    "An unhandled exception of type 'System.Data.EvaluateException' occurred in System.Data.dll
    Additional information: Cannot find column [PARTNO]."


    I am able to populate my DataGridview and everything, I just can't seem to filter it. Please help, much appreciation.





    Code:
    Imports System
    Imports System.Data.OleDb
    Public Class Form1
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Try
                Dim LinkConn As String = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\\; Extended Properties = dBase III; User ID=;Password="
                Dim con As OleDbConnection = New OleDbConnection()
                con.ConnectionString = LinkConn
                con.Open()
    
                Dim Query As String = "Select * from INV"
                Dim adapater As OleDbDataAdapter = New OleDbDataAdapter(Query, con)
                Dim ds As DataSet = New DataSet()
                adapater.Fill(ds)
                con.Close()
    
                Me.DataGridView1.DataSource = ds.Tables(0)
    
            Catch ex As Exception
                MsgBox("Error while connecting to databse." & vbNewLine & ex.Message)
    
            End Try
    
        End Sub
    
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            Dim dbDataSet As New DataTable
            Dim DV As New DataView(dbDataSet)
            DV.RowFilter = String.Format("Convert(PARTNO, 'System.String') Like '%{0}%' or DESCRIPT Like '%{0}%' or Convert(ONHAND, 'System.String') Like '%{0}%' ", TextBox1.Text)
            DataGridView1.DataSource = DV
        End Sub
    End Class

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,754

    Re: Filter .DBF File In DataGridView

    The error seems obvious, it cannot find the column 'PARTNO'. Could you post the schema of your table?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2015
    Posts
    16

    Re: Filter .DBF File In DataGridView

    Quote Originally Posted by dday9 View Post
    The error seems obvious, it cannot find the column 'PARTNO'. Could you post the schema of your table?
    Thank you for looking at this. I also thought that too, but when I run it and change this line in the button click to
    Code:
     Dim Query As String = "Select PARTNO from INV"
    it finds the column. So I maybe my syntax is wrong in the TextBoxChange? I uploaded the .DBF file that I'm using. There really isn't more to it than just that file. All I have is a Button, DataGridView and a TextBox on a Windows Form.
    Attached Files Attached Files

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,754

    Re: Filter .DBF File In DataGridView

    I'm not very good with expressions, but I think that it has to do with the way that you're using the Convert function. If you go to this page and scroll about half way down you will find the various functions. The very first function is the Convert function and here are the arguments that it accepts:

    expression -- The expression to convert.
    type -- The .NET Framework type to which the value will be converted.

    So in your example that you gave:
    Code:
    Convert(PARTNO, 'System.String')
    It is trying to convert PARTNO to System.String, but PARTNO is not an expression it is a column. My suggestion would be to remove the Convert function all together and see if it works.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Apr 2015
    Posts
    16

    Re: Filter .DBF File In DataGridView

    Code:
     DV.RowFilter = String.Format("PARTNO Like '%{0}%' or DESCRIPT Like '%{0}%' or ONHAND Like '%{0}%' ", TextBox1.Text)
    No, unfortunately I'm still getting that fancy error.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Apr 2015
    Posts
    16

    Re: Filter .DBF File In DataGridView

    I had went back in and changed the column names and still the first column can not be found.

    Code:
                Me.DataGridView1.DataSource = ds.Tables(0)
                DataGridView1.Columns(0).HeaderText = "PN"
                DataGridView1.Columns(1).HeaderText = "DS"
                DataGridView1.Columns(2).HeaderText = "OH"
                DataGridView1.Refresh()
    If anyone can help...?

  7. #7
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Filter .DBF File In DataGridView

    Quote Originally Posted by M4cJunk13 View Post
    Code:
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            Dim dbDataSet As New DataTable
            Dim DV As New DataView(dbDataSet)
            DV.RowFilter = String.Format("Convert(PARTNO, 'System.String') Like '%{0}%' or DESCRIPT Like '%{0}%' or Convert(ONHAND, 'System.String') Like '%{0}%' ", TextBox1.Text)
            DataGridView1.DataSource = DV
        End Sub
    Of course it can not find the field 'PARTNO' or any other field for that matter as there are no fields in the DataTable 'dbDataSet' (strange name also).

    You create a new empty table and use that new table as the source for the DataView.
    Last edited by TnTinMN; Apr 10th, 2015 at 05:04 PM.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Apr 2015
    Posts
    16

    Re: Filter .DBF File In DataGridView

    TnTinMN,

    I'm not quite sure I know what you mean. I'm sorry I had gotten some help from some people with this and I am fairly new to VB.NET and I'm coming from VBA environment. Could you show me an example please?

    Thank you!

  9. #9
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Filter .DBF File In DataGridView

    You have two methods. The 'Button1_Click' method queries the DB creating a DataSet that has on DataTable stored in it. When that method completes, the only access you have to the DataTable is going to be through the DataGridView1.DataSource property. This property is preserving the data you pulled because the DataGridView is declared at the Class level; this is the same concept of variable scope that exists in VBA.

    I did not check your filter logic, but this is how I would write the TextBox1_TextChanged method given your other code.

    Code:
    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
       Dim dt As DataTable = TryCast(DataGridView1.DataSource, DataTable)
       If dt IsNot Nothing Then
          If String.IsNullOrWhiteSpace(TextBox1.Text) Then
             dt.DefaultView.RowFilter = Nothing
          Else
             dt.DefaultView.RowFilter = String.Format("Convert(PARTNO, 'System.String') Like '%{0}%' or DESCRIPT Like '%{0}%' or Convert(ONHAND, 'System.String') Like '%{0}%' ", TextBox1.Text)
          End If
       End If
    End Sub

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Apr 2015
    Posts
    16

    Re: Filter .DBF File In DataGridView

    TnTinMN,

    Ok, looking at your code I see where I am making my mistake. Thank you for the example, it is definitely what I was trying to do. I'm still trying to wrap my head around some of these concepts but your snippet is clearing some of the confusion. Tank you again!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width