C# 数据表 VS 数据视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15616137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:23:11  来源:igfitidea点击:

Datatable VS dataview

c#asp.netvb.net

提问by user1187282

Hy , I really don't understand the difference between datatable and dataview since we can do :

Hy ,我真的不明白 datatable 和 dataview 之间的区别,因为我们可以这样做:

   Dim dtb As New DataTable()
   Dim dtv As DataView = dtb.DefaultView

thanks in advance .

提前致谢 。

采纳答案by Steve

The Datatableis the unordered and unfiltered collection of DataRows extracted according to your query from your database.
The DataView (and you could have more than one) is a filtered and/or ordered view of the same data.

Datatable是根据从数据库查询中提取数据行的无序和未经过滤的集合。
DataView(您可以有多个)是相同数据的过滤和/或排序视图。

For example:

例如:

 using(SqlConnection cn = GetConnection())
 {
     cn.Open();
     SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Customers", cn);
     DataTable dt = new DataTable();
     da.Fill(dt);

     // At this point dt is filled with datarows extracted from the database in no particular order 
     // And the DefaultView presents the same record organization (or lack of), but...

     // Order on the default view by CustomerName
     dt.DefaultView.Sort = "CustomerName";
     foreach(DataRowView rv in dt.DefaultView)
          Console.WriteLine(rv["CustomerName"].ToString();

     // A new dataview with only a certain kind of customers ordered by name
     DataView dvSelectedCust = new DataView(dt, "CreditLevel = 1", "CustomerName", DataViewRowState.Unchanged);
     foreach(DataRowView rv in dvSelectedCust)
          Console.WriteLine(rv["CustomerName"],ToString();



 }

Of course creating and maintaining a DataView is an hit on performances and thus you have the choice to use it only when you really need it

当然,创建和维护 DataView 会影响性能,因此您可以选择仅在真正需要时使用它

回答by Sachin

There are lot of links available of internet related to this but for summary

互联网上有很多与此相关的链接,但仅供参考

DataView is customized view of a DataTable for sorting, filtering, searching, editing, and navigation. The DataView does not store data, but instead represents a connected view of its corresponding DataTable.

DataView 是 DataTable 的自定义视图,用于排序、过滤、搜索、编辑和导航。DataView 不存储数据,而是代表其相应 DataTable 的连接视图。

You can look into simple DataView Examplein VB.

您可以在 VB 中查看简单的 DataView示例

回答by Aghilas Yakoub

DataView is additional layer that is used to filter or apply expresssion sorting etc..

DataView 是附加层,用于过滤或应用表达式排序等。

DataView contains operators such as RowFilter

DataView 包含操作符,例如 RowFilter

link : http://msdn.microsoft.com/fr-fr/library/system.data.dataview.aspx

链接:http: //msdn.microsoft.com/fr-fr/library/system.data.dataview.aspx