Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datatable VS dataview [duplicate]

Tags:

c#

asp.net

vb.net

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

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

thanks in advance .

like image 476
user1187282 Avatar asked Mar 25 '13 13:03

user1187282


People also ask

What is the difference between DataView and DataTable?

A DataView enables you to create different views of the data stored in a DataTable, a capability that is often used in data-binding applications.

What is difference between DataTable and DataSet?

DataSet comprises one or many dataset tables which have the in-memory feature. DataTable holds a single or unit database table that has an in-memory feature. DataSet is formed collectively of datatables. DataTable is composed of multiple rows and columns to have better access to data.

What is the difference between DataRow and DataRowView?

A DataRowView is a row in the DataView, so it's a special "View" of a DataRow. Represents a customized view of a DataRow. The two are not the same thing. The DataRowView object does have a Row property, which points it to the DataRow it represents.


2 Answers

The Datatable is 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.

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

like image 167
Steve Avatar answered Sep 26 '22 19:09

Steve


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.

You can look into simple DataView Example in VB.

like image 30
Sachin Avatar answered Sep 22 '22 19:09

Sachin