简体   繁体   中英

Filter Bound DataGridView by Unbound Column

I have a DataGridView bounded to a DataTable from a DataSet.xsd. I added by code one additional column called _RowString to the DataGridView . It contains a string composed by all the string values contained into the other columns of the DataTable and I want to use that column to filter the DataGridView .

My first approach was based on adding the _RowString column directly to my DataTable and not to the DataGridView . But this caused some problems when I needed to insert or update a record, because adding and calculating the column on the DataTable caused the RowState value of all the DataTable rows to be Changed and so the TableAdapterManager.UpdateAll(DataSet) method was always updating all the records!

I know that I can use a BindingSource to filter the DataGridView like I was doing before:

BindingSource.Filter = String.Format("[_RowString] LIKE '%{0}%'", FilterTextBox.Text);

But for the reason explained above, I don't want to add the _RowsString column to my DataTable object.

How can I filter the DataGridView using the _RowString unbound column without using a BindingSource and/or adding the _RowString column to my DataTable object?

Otherwise, is there a way to add a column to my DataTable without changing the RowState value?

The simplest approach may well be to include a dummy filter column in the select .

If you add a 'computed column' to your SQL , using Concat to put all necessary columns, in the right order, into one additional column you will get it right in your result set.

Example:

select *, concat(last_name, ',', first_name) as filterColumn from persons;

As a computed column it will not be updatable and you will have to take care that the correct Update statement gets created, if you need to update the table.

You can make the DataGridView column invisible or maybe even delete it..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM