Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27349

VS 2010 Filtering data in grid view.

$
0
0
I have a database "holiday.mdb" and there is a table "Properties" that contains columns "County" , "Bedrooms".
The following code shows how to filter data by one criteria( by the county OR by the number of bedrooms). But how can I filter data so that it considers both (county and number of bedrooms)? For example: selected item is "Cardiff" and number of bedrooms is "4" . Now it should filter the data in the table so that it show only places in Cardiff with 4 bedrooms.

How should I edit SQLString?
Any good tutorials for SQL statements in vb?


Imports System.Data.OleDb
Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
OleDbDataAdapter1.Fill(DataSet11)

End Sub

Private Sub lstCounties_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstCounties.SelectedIndexChanged
'Above line uses a new data object for every individual query
Dim County, SQLString As String
Dim dtProperties As New DataTable() 'Data table will fill the data grid
Dim dbDataAdapter As OleDbDataAdapter
Dim ConnectString As String = "Provider = Microsoft.Jet.OLEDB.4.0;" & "Data Source = Holidays.mdb"
County = lstCounties.Text
SQLString = "SELECT * FROM Properties WHERE County = " & "'" & County & "'" & ""
'Above, imagine Cardiff has been selected, the SQL would search for it as:
' " SELECT * FROM Properties Where County = 'Cardiff' "
dbDataAdapter = New OleDbDataAdapter(SQLString, ConnectString)
dbDataAdapter.Fill(dtProperties)
grdProperties.DataSource = dtProperties
End Sub

Private Sub lstBedrooms_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstBedrooms.SelectedIndexChanged
'Above line uses a new data object for every individual query
Dim Bedrooms, SQLString As String
Dim dtProperties As New DataTable() 'Data table will fill the data grid
Dim dbDataAdapter As OleDbDataAdapter
Dim ConnectString As String = "Provider = Microsoft.Jet.OLEDB.4.0;" & "Data Source = Holidays.mdb"
Bedrooms = lstBedrooms.Text
SQLString = "SELECT * FROM Properties WHERE Bedrooms = " & Bedrooms & ""
dbDataAdapter = New OleDbDataAdapter(SQLString, ConnectString)
dbDataAdapter.Fill(dtProperties)
grdProperties.DataSource = dtProperties

End Sub

End Class

Viewing all articles
Browse latest Browse all 27349

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>