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].
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?
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
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
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
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?
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
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
Re: Error [Column not found] Filtering datagridview with textbox
You would clear the filter on the BindingSource using the RemoveFilter method.
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?
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.