HI All,

Probably a simple question - but im a noob here.

I am building a website using asp.net in VS 2005 using c#. i have a grid view that is pulling values from a table on my sql database. All fine so far, but what i would like to do is filter these results by values entered in text boxes.

For example, there is a textbox for ID number. i would like the user to enter a value in to this box and for the gridview to filter its results by the value in this field.

I built the query that brings this data in to the gridview with the query builder wizard , but i couldnt see in there how to link the SQL query WHERE declarations to a field on my form.

Ideally what i want to see is:

select * from customers
where ID = textbox1.value

Is this indeed possible?

Many Thanks

Recommended Answers

All 4 Replies

First, this should be in the Web Development > ASP.Net forum :)

That being said, you will need some sort of a trigger event to populate the gridview once the textBox has information in it or if it is populated based off the textBox value at page load it will error out unless you have a default SELECT statement that runs at page load.

How I would handle it is to have the data connection, SELECT process and gridView population triggered via a button click.

Your select statement part of your connection procedure can be phrased as:
"SELECT * FROM customers WHERE ID = '" + textBox1.Text + "'"

The rest I leave in your capable hands.

Hope this helps :) Please mark as solved if it resolves your issue.

I work with an access mdb
I used your filtering advice and when I run the program it gives an error “Nullreference exception unhandled” “De objectverwijzing is niet op een exemplaar van een object ingesteld”

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 & "*'")


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

End Sub

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.