|
-
Jun 23rd, 2014, 09:55 AM
#1
Thread Starter
Junior Member
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].
-
Jun 23rd, 2014, 10:00 AM
#2
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?
-
Jun 23rd, 2014, 10:10 AM
#3
Thread Starter
Junior Member
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
-
Jun 23rd, 2014, 10:28 AM
#4
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
-
Jun 23rd, 2014, 10:51 AM
#5
Thread Starter
Junior Member
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
-
Jun 23rd, 2014, 10:54 AM
#6
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?
-
Jun 23rd, 2014, 11:55 AM
#7
Thread Starter
Junior Member
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
-
Jun 23rd, 2014, 12:02 PM
#8
Thread Starter
Junior Member
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
-
Jun 23rd, 2014, 12:34 PM
#9
Re: Error [Column not found] Filtering datagridview with textbox
You would clear the filter on the BindingSource using the RemoveFilter method.
-
Jun 23rd, 2014, 01:30 PM
#10
Thread Starter
Junior Member
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?
-
Jun 23rd, 2014, 01:37 PM
#11
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|