Monday, September 23, 2013

Difference between DataTable and DataView

Simply, 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.

when you want to run a query and show the subset of data in a control, a DataView could help you.

DataTable

A datatable is an in-memory representation of a single database table. You can think of it as having columns and rows in the same way. The DataTable is a central object in the ADO.NET library. Other objects that use the DataTable include the DataSet and the DataView.

DataView

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. For example, one control may be bound to a DataView showing all of the rows in the table, while a second may be configured to display only the rows that have been deleted from the DataTable. The DataTable also has a DefaultView property which returns the default DataView for the table.

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();



 }

Difference between DataView and DataTable
DataView

1.Read-only i.e., DataView can be used to select the data.
2.Is a reference to an existing DataTable. Cannot be populated from scratch; must be instantiated with a reference to an existing DataTable.
3.Data is a reference to an existing DataTable, and does not consume space.
4.Can sort or filter rows without modifying the underlying data. Rows and columns can be hidden and revealed repeatedly.
5.Can return a DataTable version of the view
6.A live reference to a DataTable; any changes in the DataTable data is immediately reflected in the view.
7.Supports calculated columns, which are columns with a value calculated on the fly by combining or manipulating other columns.
8.Can hide or show selected columns

DataTable

1.Read/Write i.e., Datatable can be used to edit or select or delete or insert a data.
2.Can be created empty and then populated
3.Data takes storage space.
4.Can add/edit/delete rows, columns, and data, and all changes are persistent.
5.Can be cloned
6.Is source data; does not contain references
7.Does not support calculated columns
8.No row or column hiding


Thanks,
Reference from Forums,Sites.google,Stackoverflow.

No comments:

Post a Comment