Hey guys, I'm doing a project for university and i have hit a wall. I have a gridview that is connected to a database as shown below:

http://i40.tinypic.com/fys9i.jpg
(menu is incomplete)

I want a drop down box that is populated with the type of food such as (pizzas, burgers etc etc). When the user selects one of the items within the drop down box, how would i filter the gridview to only show the selected type of food? EG
If i was to select Pizzas i would only want the pizzas to be shown..

How would i go about doing this?
Thanks

Recommended Answers

All 5 Replies

Where come these data of yours that populates gridview? Is gridview databound?
Tell us more, so we can help you more.

Ok, I think you should use the following technique: using dataTable and your main data source, from where all queries will be done, by using some linq.
Take a look at here:

Private table As New DataTable("myTable")
Public Sub New()
	InitializeComponent()
	table.Columns.Add("ID", GetType(String))
	table.Columns.Add("Type", GetType(String))
	table.Columns.Add("Name", GetType(String))
	table.Columns.Add("Description", GetType(String))

	table.Rows.Add("1", "Pizza", "Pizza 1", "description 1")
	table.Rows.Add("2", "Burger", "Burger 1", "description 2")
	table.Rows.Add("3", "Pizza", "Pizza 2", "description 3")
	table.Rows.Add("4", "Meat", "Meat 1", "description 4")
	table.Rows.Add("5", "Burger", "Burger 2", "description 5")

	'get only distinct names from table, to show in comboBox:
	Dim items As IEnumerable(Of String) = table.AsEnumerable().[Select](Function(s) DirectCast(s("Type"), String)).Distinct()
	'set datasource to combobox:
	comboBox1.DataSource = New BindingSource(items, Nothing)
	'set datasource to dgv:
	dataGridView1.DataSource = New BindingSource(table, Nothing)
End Sub

Private Sub comboBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
	Dim rows As EnumerableRowCollection(Of DataRow) = table.AsEnumerable().Where(Function(w) DirectCast(w("Type"), String) = comboBox1.SelectedItem.ToString()).OrderBy(Function(o) o("Name"))
	Dim dataView As DataView = rows.AsDataView()
	dataGridView1.DataSource = New BindingSource(dataView, Nothing)
End Sub

Code is proven working, its tested.
I hope you like it.

bye

Ok, I think you should use the following technique: using dataTable and your main data source, from where all queries will be done, by using some linq.
Take a look at here:

Private table As New DataTable("myTable")
Public Sub New()
	InitializeComponent()
	table.Columns.Add("ID", GetType(String))
	table.Columns.Add("Type", GetType(String))
	table.Columns.Add("Name", GetType(String))
	table.Columns.Add("Description", GetType(String))

	table.Rows.Add("1", "Pizza", "Pizza 1", "description 1")
	table.Rows.Add("2", "Burger", "Burger 1", "description 2")
	table.Rows.Add("3", "Pizza", "Pizza 2", "description 3")
	table.Rows.Add("4", "Meat", "Meat 1", "description 4")
	table.Rows.Add("5", "Burger", "Burger 2", "description 5")

	'get only distinct names from table, to show in comboBox:
	Dim items As IEnumerable(Of String) = table.AsEnumerable().[Select](Function(s) DirectCast(s("Type"), String)).Distinct()
	'set datasource to combobox:
	comboBox1.DataSource = New BindingSource(items, Nothing)
	'set datasource to dgv:
	dataGridView1.DataSource = New BindingSource(table, Nothing)
End Sub

Private Sub comboBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
	Dim rows As EnumerableRowCollection(Of DataRow) = table.AsEnumerable().Where(Function(w) DirectCast(w("Type"), String) = comboBox1.SelectedItem.ToString()).OrderBy(Function(o) o("Name"))
	Dim dataView As DataView = rows.AsDataView()
	dataGridView1.DataSource = New BindingSource(dataView, Nothing)
End Sub

Code is proven working, its tested.
I hope you like it.

bye

Thanky you for the reply. But it doesnt really do what i asked for. If the user selects "Pizza" i want them to only be able to see the pizzas. Hope that makes sense?

Where come these data of yours that populates gridview? Is gridview databound?
Tell us more, so we can help you more.

Yes it is databound.

Sure it does. Try using ONY my code. You will see it works.
Otheriwse, try using your own brains while looking into my code - you will see what Linj queries are getting. Try chaning them a bit - if my code is not exactly what you are lookin for.
If you still have problems, let me now, i`ll try to help you out, ...
but 1st try to find the solution just by your self - its not fat off now.

bye

hello!
you can do like this

dim con as new sqlconnection("connection string")
con.open()
dim da as new sqldataapter("select * from foods where foodtype='"&combo.text&"'",con)
dim dt as new datatable
da.fill(dt)
gridview.datasource = dt
con.close()

use this code at the button click event you will find all the records of the selected type , and if you want to populate your combo also then use this code at the load event of your form

dim con as new sqlconnection("connection string")
con.open()
dim da as new sqldataapter("select foodType from foods",con)
dim dt as new datatable
da.fill(dt)
Combo.datasource = dt
combo.displaymember = "foodType"
con.close()

Regards

commented: good work +0
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.