Jump to content

DataGridView filter with ComboBox

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
vlahob

vlahob

    Newbie

  • Members
  • Pip
  • 1 posts
Hi!
I have a little problem:
I have a DataGridView and I just filled it with data from source:
this.usedfunc_TableAdapter.Fill(this.mSDOCSDataSet._usedfunc_);
and I created a ComboBox with databound, and it show the first column from the DataGridRow.
I just like to reach when I'm selecting something from the ComboBox, the DataGridView just show that 1 row.

Sry for the annoying question, but im new in programming!

#2
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
The solution I'll propose requires that DataGridView is not intended to be used to add new rows to the table. If so, the solution is like this:

1. Create DataView object and set its DataSource property to DataTable you used as data source for the DataGridView.
2. Set DataView's RowFilter to empty string (i.e. show all rows).
3. Set DataGridView's DataSource property to DataView object
4. Whenever Combo box selected item changes, set DataView's RowFilter to "IdField=value", where IdField is the field by which you filter and value is the selected value (don't forget single quotes for character fields).

That will make all other rows disappear from the grid and just this one remain.

This is general technique and you can use it to filter by any criteria (e.g. by price range if you have column showing prices, etc.).

Edited by zoranh, 13 July 2010 - 06:38 AM.
Corrected typo