Filter the BindingSource based on the rows of another DataGridView

I have two DataGridViewsin Winforms. DataGrid1 is connected to a table containing a list of tasks to be completed. When someone shuts down, he enters into a separate table as completed, which is associated with DataGrid2.

I need to filter the binding source for DataGrid1 so that when the job is completed in DataGrid2, it is filtered out from DataGrid1. The current code that I use only filters the binding source by the last record in DataGrid2, and I need to filter it with all the elements.

How to filter BindingSource for DataGrid1 based on all values โ€‹โ€‹of a DataGrid2 column?

foreach (DataGridViewRow row in dataGrid2.Rows)
{
    DataGrid1BindingSource.Filter = 
        string.Format("ColumnName <> '{0}'", row.Cells[1].Value);
}

Here is an example of all the jobs in the data table, then the first grid that contains the incomplete jobs, and the second grid that contains the completed jobs. Jobs that should be displayed in an incomplete grid are those jobs that are not in the Completed Jobs grid:

 __________        ____________        ___________
| All Jobs |      | Incomplete |      | Completed |  
|โ€•โ€•โ€•โ€•โ€•โ€•โ€•โ€•โ€•โ€•|      |โ€•โ€•โ€•โ€•โ€•โ€•โ€•โ€•โ€•โ€•โ€•โ€•|      |โ€•โ€•โ€•โ€•โ€•โ€•โ€•โ€•โ€•โ€•โ€•|  
| JobTitle |      | JobTitle   |      | JobTitle  |  
|โ€•โ€•โ€•โ€•โ€•โ€•โ€•โ€•โ€•โ€•|      |โ€•โ€•โ€•โ€•โ€•โ€•โ€•โ€•โ€•โ€•โ€•โ€•|      |โ€•โ€•โ€•โ€•โ€•โ€•โ€•โ€•โ€•โ€•โ€•|  
| Job 1    |      | Job 1      |      | Job 3     |  
| Job 2    |      | Job 2      |      | Job 4     |  
| Job 3    |      |            |      |           |
| Job 4    |      |            |      |           |
 โ€พโ€พโ€พโ€พโ€พโ€พโ€พโ€พโ€พโ€พ        โ€พโ€พโ€พโ€พโ€พโ€พโ€พโ€พโ€พโ€พโ€พโ€พ        โ€พโ€พโ€พโ€พโ€พโ€พโ€พโ€พโ€พโ€พโ€พ
+4
source share
2 answers

, , , bool -, , , . . , bool, . Completed = true Completed = false.

IN . , , , :

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));

, int, , , "Column1 NOT IN (1,2,3)" . "Column1 NOT IN ('a','b','c')". select, :

.Select(r => string.Format("'{0}'",r.Cells[0].Value.ToString()));
+3

, , :

var colname = YOURGRIDTOFILTER.Columns[INDEXOFCOLUMNTOFILTER].HeaderText;
            var filterString = colname+" <> ";
            foreach (DataGridViewRow row in dataGrid2.Rows)
            {
                filterString += "'" + row.Cells[1].Value + "' OR "+colname+" <> ";
            }
            filterString = filterString.Substring(0, filterString.LastIndexOf("OR"));
0

Source: https://habr.com/ru/post/1648368/


All Articles