I am rather new here and have already a good (I hope so) question.

I work with an access mdb
When I run the program with the following code it gives an error “Nullreference exception unhandled” “the object… is not for an example of an object…” (very free translated but you will surely recognize this)

This is my code:

Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged
Dim dvFilter As DataView = Nothing

If TypeOf DagverslagDataGridView.DataSource Is DataView Then
dvFilter = CType(DagverslagDataGridView.DataSource, DataView)
ElseIf TypeOf DagverslagDataGridView.DataSource Is DataTable Then
dvFilter = CType(DagverslagDataGridView.DataSource, DataTable).DefaultView

End If
If TextBox5.TextLength > 0 Then
dvFilter.RowFilter = ("select * from Dagverslag WHERE Feit LIKE '*" & TextBox5.Text & "*'")

dvFilter.RowFilter = ""
End If
DagverslagDataGridView.DataSource = dvFilter

End Sub

My question is: What am I doing wrong ?

I can see you use DataTable as a binding source to the dgv control.
So you can use filtering on the dataTable, with a help of "SELECT" method:

Private table As DataTable
Public Sub New()
	table = New DataTable("MyTable")
	table.Columns.Add("Id", GetType(Integer))
	table.Columns.Add("Name", GetType(String))
	table.Columns.Add("Car", GetType(String))

	table.Rows.Add(1, "Name 1", "Ferrari")
	table.Rows.Add(2, "Name 2", "Jaguar")
	table.Rows.Add(3, "Name 3", "Ferrari")
	table.Rows.Add(4, "Name 4", "Porsche")
	table.Rows.Add(5, "Name 5", "Ferrari")
	table.Rows.Add(6, "Name 6", "Porsche")

	dataGridView1.DataSource = New BindingSource(table, Nothing)
End Sub

Private Sub button1_Click(sender As Object, e As EventArgs)
	Dim myFilter As String = "Ferrari"
	Dim newTable As DataTable = New DataView(table, "Car = '" & myFilter & "'", "Name DESC", DataViewRowState.CurrentRows).ToTable()
	dataGridView1.DataSource = Nothing
	dataGridView1.DataSource = New BindingSource(newTable, Nothing)
End Sub

NOTE: "Car = 'Ferrari' is an actual filter. So in your case myFilter variable is set to some textBox, which will be used to filter dgv.

Edited 5 Years Ago by Mitja Bonca: n/a

It seems to me you can work out things a lot better in C# than in basic. I will surely give this a try.


This question has already been answered. Start a new discussion instead.