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

Asked By Aldo Liaks on 29-Jun-08 07:25 AM

Hi guys,

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 or
2. DataTable or Dataset or
3. Binding Source or
4. 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,

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.datasource.aspx


C# Tutorial - Binding a DataGridView to a Database

http://blog.paranoidferret.com/index.php/2007/09/05/c-tutorial-binding-a-datagridview-to-a-database/

Try this aldo - Sagar P replied to Aldo Liaks on 30-Jun-08 12:19 AM

To form a RowFilter value, specify the name of a column followed by an operator and a value to filter on. The value must be in quotes. For example:

"LastName = 'Smith'"

Consider that you have all data you want in ds and u want to apply rowfilter on col1;

    Dataview dVi = ds.Tables[0].DefaultView;

     dVi.RowFilter = "col1 = " + value;

Or see this example;

private void MakeDataView()
{
   DataView dv = new DataView();

   dv.Table = DataSet1.Tables["Suppliers"];
   dv.AllowDelete = true;
   dv.AllowEdit = true;
   dv.AllowNew = true;
   dv.RowFilter = "City = 'Berlin'";
   dv.RowStateFilter = DataViewRowState.ModifiedCurrent;
   dv.Sort = "CompanyName DESC";
  
   // Simple bind to a TextBox control
   Text1.DataBindings.Add("Text", dv, "CompanyName");
}

Also go thr these links to know more abt that;

http://msdn.microsoft.com/en-us/library/system.data.dataview.rowfilter(VS.71).aspx

http://www.akadia.com/services/dotnet_filter_sort.html

Best Luck!!!!!!!!!!!!!!!!
Sujit.

 

SOLVED! - Aldo Liaks replied to Sagar P on 30-Jun-08 05:18 AM

Hi guys,

The code below works for me!

void FilterBy(ExtendedDataGridView dGV)

{

DataSet ds = (DataSet)dGV.DataSource;

DataView dv = ds.Tables[0].DefaultView;

dv.RowFilter = dGV.Columns[0].HeaderText + " = " + "408";

dGV.DataSource = dv;

}

Thanks a lot!

Aldo.

Re :: Filter data in DataGridView - RowFilter Property - Shailendrasinh Parmar replied to Aldo Liaks on 30-Jun-08 05:09 AM
See the following articles ::

http://

Hope it helps.

Find this reply - Nadaraja Yogendran replied to Aldo Liaks on 30-Jun-08 07:14 AM
Object reference not set to an instance of an object - kee replied to Nadaraja Yogendran on 15-Jul-10 10:03 PM

DataView dv = ds.Tables[0].DefaultView;

it gives this error : Object reference not set to an instance of an object. Can you tell me what does this mean ?

kee replied to Aldo Liaks on 15-Jul-10 10:04 PM

DataView dv = ds.Tables[0].DefaultView;

it gives this error : Object reference not set to an instance of an object. Can you tell me what does this mean ?

error - kee replied to Aldo Liaks on 15-Jul-10 10:05 PM

DataView dv = ds.Tables[0].DefaultView;


it gives this error : Object reference not set to an instance of an object. Can you tell me what does this mean ?

asdfsf replied to Aldo Liaks on 09-Feb-11 05:20 AM
nagnu man kah!