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 .
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With