如何获取DataView中被RowFilter过滤掉的数据集
获取DataView过滤后被排除的记录集合
Got it, let's break down how to grab the records that got filtered out of your DataView. The core idea is to compare the full set of rows from the original table against the rows that are visible in the filtered DataView—then the difference is exactly what you need.
Step-by-Step Solution
Here's a clean, LINQ-based approach:
- First, get all rows from the original underlying table of your DataView.
- Then extract the rows that are visible in the filtered DataView (since DataView contains
DataRowViewobjects, we'll map them back to their originalDataRowinstances). - Use the
Exceptmethod to find rows that exist in the full table but not in the visible set—those are your excluded records.
Full Code Example
// Assume your DataView is stored in this variable DataView filteredView = data as DataView; // Get all rows from the original table var allTableRows = filteredView.Table.AsEnumerable(); // Get only the rows that are visible in the filtered DataView var visibleRows = filteredView.Cast<DataRowView>() .Select(rowView => rowView.Row); // Calculate the excluded records (full table minus visible rows) var excludedRecords = allTableRows.Except(visibleRows);
Bonus: Convert Excluded Records to a DataTable
If you need the excluded records in a new DataTable (instead of just an enumerable), you can use:
DataTable excludedTable = excludedRecords.CopyToDataTable();
Why This Works
DataRowView.Rowdirectly references the originalDataRowfrom the table, so theExceptmethod can accurately compare references to find the missing rows.- This approach avoids duplicating your original
RowFilterlogic manually, making it maintainable and efficient.
内容的提问来源于stack exchange,提问作者Adhi
相关产品推荐
相关产品推荐

