c# - Filter a BindingSource based on the rows of another DataGridView -
i have 2 datagridviews
in winforms. datagrid1 connected table contains list of jobs need completed. once completes job, it's entered separate table completed, connected datagrid2.
i need filter binding source datagrid1 when job shows completed in datagrid2 it's filtered out of datagrid1. current code i'm using filters binding source last entry in datagrid2 , need filter of entries.
how filter bindingsourc
e datagrid1 based on values of column of datagrid2?
foreach (datagridviewrow row in datagrid2.rows) { datagrid1bindingsource.filter = string.format("columnname <> '{0}'", row.cells[1].value); }
here example of jobs in data table, first grid contains incomplete jobs , second grid contains completed jobs. jobs should shown in incomplete grid, jobs not in completed jobs grid:
__________ ____________ ___________ | jobs | | incomplete | | completed | |――――――――――| |――――――――――――| |―――――――――――| | jobtitle | | jobtitle | | jobtitle | |――――――――――| |――――――――――――| |―――――――――――| | job 1 | | job 1 | | job 3 | | job 2 | | job 2 | | job 4 | | job 3 | | | | | | job 4 | | | | | ‾‾‾‾‾‾‾‾‾‾ ‾‾‾‾‾‾‾‾‾‾‾‾ ‾‾‾‾‾‾‾‾‾‾‾
before reading answer, should know if don't have bool
field or detect job completed it's not design. should have single list of jobs. based on bool
field should show incomplete jobs in first grid , completed jobs in second grid. filter completed = true
, completed = false
.
anyway, can use in
in filter expression. it's enough create list of values want use in filter, create filter way:
var ids = this.datagridview2.rows.cast<datagridviewrow>() .where(r => !r.isnewrow) .select(r => r.cells[0].value.tostring()); bs1.filter = string.format("column1 not in ({0})", string.join(",", ids));
in above example supposed ids int
example "column1 not in (1,2,3)"
filter. string ids, filter "column1 not in ('a','b','c')"
. can change select statement below:
.select(r => string.format("'{0}'",r.cells[0].value.tostring()));
Comments
Post a Comment