DataTable 的新 DataView 与 DefaultView

New DataView vs. DefaultView of a DataTable

为什么要构造一个新的 DataView 而不是使用 C# 中 DataTableDefaultView

在什么情况下创建新的 DataView 更可取?

两者的优缺点是什么?

var dataView = new DataView(dataTable);

var dataView = dataTable.DefaultView;

顾名思义,DefaultView 的优点是默认已经存在。

其他 DataViews 的优势在于,您可以让其中的几个准备就绪并并行使用

所以你可以 filtersort 其中 3 个以不同的方式绑定 3 个不同的控件,例如three DataGridViews或一个DGV和一个ComboboxCellItems对他们独立

引用自this post

A dataview is a view on a datatable, a bit like a sql view. It allows you to filter and sort the rows - often for binding to a windows form control. Additionally, a DataView can be customized to present a subset of data from the DataTable. This capability allows you to have two controls bound to the same DataTable, but showing different versions of the data.

另一种情况是创建一个新的 DataView 是更可取的,它是在会话之间共享的 asp 全局(应用程序变量)数据表。 Defaultview with rowfilter 不是可取的,因为应用的过滤器会影响所有会话 defaultview。所以你必须为每个会话创建数据视图。 vb.net

Application("dt") = New DataTable() - persits across sessions
Application("dt").DefaultView.RowFilter="Field = Value" - not preferable because it apply all sessions
Session("dv") = New DataView(Application("dt"))
Session("dv").RowFilter="Field = Value" - preferable