I have a binded datagridView and i want to filter it using a TextBox value.
I used this code:
Oct 22, 2007 - I have a datagridview on a form and It has two columns which are comboboxes. The columns are use for showing name, lastname. The DataGridView has no filtering options of his own. When in the past I was truely unable to put a filter on the DataSource, I used to just hide the rows that didn't meet my requirements. Check out this class, it extends the current DataGridView with custom filters.
But this code doesn't filter the datagridView even i have used the same code
in another datagridView and it works perfectly. I don't know where is the
error in my code?
Thanks in advance.
EDIT:
i removed try catch and i got this error message:
unable to cast object of type 'system.windows.forms.bindingsource' to type 'system.data.datatable'
How can i fix it??
user4428204
user4428204user4428204562 gold badges4 silver badges11 bronze badges
2 Answers
The DataSource is a type of BindingSource not DataTable, so try this code:
user4340666user4340666
I think you need to use this Method :
All you need is change the Columns Number using a Combobox for Example ... just use your imagination .
Thank you .
nassimlouchaninassimlouchani
Not the answer you're looking for? Browse other questions tagged c#winformsdatagridviewfilter or ask your own question.
I'm developing user control in C# Visual Studio 2010 - a kind of 'quick find' textbox for filtering datagridview. It should work for 3 types of datagridview datasources: DataTable, DataBinding and DataSet.My problem is with filtering DataTable from DataSet object, which is displayed on DataGridView.
There could be 3 cases (examples for standard WinForm application with DataGridView and TextBox on it) - first 2 are working OK, I've problem with 3rd one:
1. datagridview.DataSource = dataTable : it works
so I can filter by setting: dataTable.DefaultView.RowFilter = 'country LIKE '%s%';
so I can filter by setting: dataTable.DefaultView.RowFilter = 'country LIKE '%s%';
2. datagridview.DataSource = bindingSource: it works
so I can filter by setting: bindingSource.Filter = 'country LIKE '%s%';
so I can filter by setting: bindingSource.Filter = 'country LIKE '%s%';
3. datagridview.DataSource = dataSource; datagridview.DataMember = 'TableName': it doesn't work
It happens when you design a table using designer: put the DataSet from toolbox on form, add dataTable to it and then set datagridview.DataSource = dataSource; and datagridview.DataMember = 'TableName'.
Code below pretends these operations:
It happens when you design a table using designer: put the DataSet from toolbox on form, add dataTable to it and then set datagridview.DataSource = dataSource; and datagridview.DataMember = 'TableName'.
Code below pretends these operations:
If you test it - although datatable is filtered (ds.Tables[0].DefaultView.Count changes), datagridview is not updated...I've been looking for a long time for any solution, but the problem is that DataSource cannot change - as it's additional control, I don't want it to mess up with programmer's code.
I know possible solutions are:
- to bind DataTable from DataSet using DataBinding and use it as example 2: but it's up to the programmer during code writing,
- to change dataSource to BindingSource, dataGridView.DataSource = dataSet.Tables[0], or to DefaultView programatically: however, it changes the DataSource. So the solution:
- to bind DataTable from DataSet using DataBinding and use it as example 2: but it's up to the programmer during code writing,
- to change dataSource to BindingSource, dataGridView.DataSource = dataSet.Tables[0], or to DefaultView programatically: however, it changes the DataSource. So the solution:
is not acceptable, as you see on MessageBox's dataSource is changing...
I don't want to do that, because it's possible a programmer writes code similar to this:
He can do that, as he designed DataGridView with DataSet and DataMember in designer.Code will be compiled, however, after using a filter, it will throw an exception...
So the question is: how can I filter DataTable in DataSet and show the results on DataGridView without changing DataSource to another? Why I can filter DataTable from example 1 directly, while filtering DataTable from DataSet is not working?Maybe it's not DataTable bound to DataGridView in that case?
Please note, that my problem takes from designing issues, so the solution MUST WORK on example 3.
mj82mj823,5785 gold badges26 silver badges38 bronze badges
7 Answers
I just spent an hour on a similar problem. For me the answer turned out to be embarrassingly simple.
Brad BruceBrad Bruce6,3713 gold badges31 silver badges56 bronze badges
I developed a generic statement to apply the filter:
The square brackets allow for spaces in the column name.
Additionally, if you want to include multiple values in your filter, you can add the following line for each additional value:
Joe SiskJoe Sisk4371 gold badge6 silver badges16 bronze badges
A simpler way is to transverse the data, and hide the lines with the
Visible
property.Just an idea... it works for me.
Guillermo Gutiérrez10.8k13 gold badges73 silver badges99 bronze badges
João MoreiraJoão Moreira
You could create a DataView object from your datasource. This would allow you to filter and sort your data without directly modifying the datasource.
Also, remember to call
TechnicalKalsadataGridView1.DataBind();
after you set the data source.1,5441 gold badge13 silver badges21 bronze badges
epotterepotter
4,8286 gold badges56 silver badges84 bronze badges
//'Comment' Filter datagrid without changing dataset,Perfectly works.
SPrSPr
I have a clearer proposal on automatic search in a DataGridView
this is an example
KOUAKEP ARNOLDKOUAKEP ARNOLD
I found a simple way to fix that problem. At binding datagridview you've just done:
datagridview.DataSource = dataSetName.Tables['TableName'];
If you code like:
the datagridview will never load data again when filtering.
displayName9,6014 gold badges37 silver badges60 bronze badges
Quyền Đặng QuangQuyền Đặng Quang