i have a dgvw in my winform . I have a textbox to search/filter the dgvw.I tried a lots of code to filter the dgvw.But each one came up with different problems.Now,i am trying to use this code :
Private Sub searcgcon_TextChanged(sender As Object, e As EventArgs) Handles searcgcon.TextChanged
TryCast(userdatagrid.DataSource, DataTable).DefaultView.RowFilter = String.Format("Name LIKE '{0}%' OR Name LIKE '% {0}%'", searcgcon.Text)
I applied this code without understanding . Now it gives me an error saying : Object reference not set to an instance of an object. I know i missed something.e.g. In the textchange event i applied this code, bu t as the code mentions "Userdatagrid.Datasource,DataTable",i did not declare/setup a datatable within the textchange event.Moreover,the code has """Name LIKE '{0}%' OR Name LIKE "".now here, is "NAME" the name of my db's column ?(i have multiple columns) Now on my form's load event,the dgvw loads data from a sql db.Here's the code :
Private Sub Contact_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim cmd As New SqlCommand("Select * from Contacts", con)
Dim adapter As New SqlDataAdapter(cmd)
Dim table As New DataTable
adapter.Fill(table)
userdatagrid.DataSource = table
So, i just wanna know: 1. what am i missing ? 2. the code that i'm using to filter, is it the right code/solution ? 3. As mentioned, i have multiple columns in my db/dgvw,so how do i configure the filtering code to search in all the columns ? 4. About the USERDATAGRID.DATASOURCE,DATATABLE..i mentioned i did not set up any datatable within the textchane event , so can any one guide me what to do ?