C# .NET - C# 3.0 - Filter data in a DataGridView - RowFilter property

Can anyone tell me how do I use the property "Rowfilter" applied to a DataGridView?

I need to build somekind of filter, to filter column data in myDGV.

I am looking for something like:

myDGV.RowFilter("myColumn", 'filter by');

but i can't find it.

I saw that RowFilter is actually a property of DataView class, but how don't where to start...

Thanks in advance,

Aldo.

Rowfilter - sundar k replied to Aldo Liaks on 29-Jun-08 08:25 AM

You just have to set the Rowfilter property of Dataview to filter your rows based on selected field, in the below example the DataView object is binding to the DataTable.DefaultView property, which is all the columns and rows in your table with a DataRowState equal to CurrentRows.

DataView dv = myDataTable.DefaultView;

Now apply your Rowfilter to Dataview object and set the dataview as datasource to your datagridview1 object.

dv.RowFilter = "ID = 100";dataGridView1.DataSource = dv;

Check the below article for more info,

http://davidhayden.com/blog/dave/archive/2006/02/11/2798.aspx

myDataTable - Aldo Liaks replied to sundar k on 29-Jun-08 10:07 AM

Hi Sundar!

What is myDataTable in order to myDataGridView? (DataMember maybe?)

Thanks,

Aldo.

yes - sundar k replied to Aldo Liaks on 29-Jun-08 01:37 PM

Yes, DataMember returns the name of the datatable in the DataSource for which the DataGridView is displaying data.

You would have set the Datagridview.Datasource to some valid datasources like,1. List - one dimensional arrays or2. DataTable or Dataset or3. Binding Source or4. Binding List

Typically, you will bind to a BindingSource component and bind the BindingSource component to another data source or populate it with business objects. The BindingSource component is the preferred data source because it can bind to a wide variety of data sources and can resolve many data binding issues automatically.

When binding to a data source that contains multiple lists or tables, you must set the DataMember property to a string that specifies the list or table to bind to. When binding to a BindingSource component that contains multiple lists or tables, however, you can set the DataMember property of the BindingSource component instead.

Are you binding your Gridview to your Binding Source? if yes, why dont you try setting the Datatable directly to your Datagridview?

If you are using Bindingsource, You will set your corresponding Datatable to your Bindingsource Datasource property and then set that bindingsource object to your Datagridview.datasource property.

You can check the below msdn link for more info on Datagridview.datasource property,