Results 1 to 11 of 11

Thread: Error [Column not found] Filtering datagridview with textbox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2014
    Posts
    21

    Question Error [Column not found] Filtering datagridview with textbox

    I am using this code

    Code:
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
            Dim dbDataSet As New DataTable
            Dim DV As New DataView(dbDataSet)
            DV.RowFilter = String.Format("PartNumber Like '%{0}%'", txtSearch.Text)
            DataGridView1.DataSource = DV
        End Sub
    I get the error - Cannot find column [PartNumber].

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: Error [Column not found] Filtering datagridview with textbox

    This one is pretty obvious, but let's set through your code.

    Code:
    Dim dbDataSet As New DataTable
    Here you declare a new DataTable.

    Code:
    Dim DV As New DataView(dbDataSet)
    Here you declare a new DataView and set it's table to the DataTable declared a line before it.

    Code:
    DV.RowFilter = String.Format("PartNumber Like '%{0}%'", txtSearch.Text)
    Here you set a filter to where PartNumber is like some value in a textbox

    Code:
    DataGridView1.DataSource = DV
    Here you set the DataSource of your DataGridView to the DataView declared earlier.

    Where in this code do you add a DataColumn named PartNumber to your DataTable?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2014
    Posts
    21

    Re: Error [Column not found] Filtering datagridview with textbox

    It already part of the datagrid, as in I added it to the datagridview through the properties window

    Just a disclaimer I'm relatively new to Visual Basic

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: Error [Column not found] Filtering datagridview with textbox

    If that is the case then you should restructure how you're currently doing it.

    Rather than adding a column during design time(through the properties windows), you should add your DataTable and any other columns via code. You should create a new DataTable, add any DataColumns to that DataTable, create a new BindingSource, add the DataTable to the BindingSource, bind the BindingSource to the DataGridView. The you'd filter the BindingSource.

    Here is an example:
    Code:
    Public Class Form1
    
        Dim bs As BindingSource
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Create a new DataTable
            Dim dt As DataTable = New DataTable
    
            'Add the PartNumber column
            dt.Columns.Add(New DataColumn("PartNumber"))
    
            'Create a new BindingSource
            bs = New BindingSource
            'Bind the DataTable to the BindingSource
            bs.DataSource = dt
    
            'Bind the BindingSource to the DataGridView
            DataGridView1.DataSource = bs
        End Sub
    
        Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
            'If the text is not empty
            If Not String.IsNullOrWhiteSpace(txtSearch.Text) Then
                'Set the filter
                bs.Filter = String.Format("PartNumber Like '%{0}%'", txtSearch.Text)
            End If
        End Sub
    End Class
    Last edited by dday9; Jun 23rd, 2014 at 10:35 AM. Reason: Seperated the paragraphs
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2014
    Posts
    21

    Re: Error [Column not found] Filtering datagridview with textbox

    Now I am getting the error

    Object reference not set to an instance of an object.

    Over the bs.Filter = String.Format("PartNumber Like '%{0}%'", txtSearch.Text) line

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: Error [Column not found] Filtering datagridview with textbox

    That means that something in that statement is not declared. Is your BindingSource declared and do you have a textbox named txtSearch?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jun 2014
    Posts
    21

    Re: Error [Column not found] Filtering datagridview with textbox

    I do have a textbox named txtsearch, I also have a public declaration of the Binding Source at the top - Dim bs As BindingSource

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jun 2014
    Posts
    21

    Re: Error [Column not found] Filtering datagridview with textbox

    Yay thank you so much I got it to work, now how can I clear the search so that all the items appear again

  9. #9
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: Error [Column not found] Filtering datagridview with textbox

    You would clear the filter on the BindingSource using the RemoveFilter method.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Jun 2014
    Posts
    21

    Re: Error [Column not found] Filtering datagridview with textbox

    Thanks again,
    I just figured out how to import an excel spreadsheet into the datagridview, the search works before the excel import but not after the excel import, why is that?

  11. #11
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: Error [Column not found] Filtering datagridview with textbox

    Please start a new thread as that question is not related to the original question asked.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

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