In earlier article you have learned DataTable and DataSet, now you will learn about Ado.net DataView, which is very similar object in Ado.net with some Differences.
DataView is more like view only representation of a DataTable, just like how you use View in Sql Database, But in SQL View we can fetch data from different table and even from different database objects, but Ado.Net DataView has some limitation, we can create different DataView object from one DataTable only, with DataView you can sort, filter the rows, DataView also can be considered as subset of Data.
DataView is used for data binding on both Web Forms and Windows Forms, DataView can be customised, DataView can be customised, and each DataTable has a DataView property
DataView _dv = new DataView();
Let's look at some DataView example. In this DataView example we will be using a DataTable with some data, you can create a DataTable with some static data to understand how DataView works
Here we have written a function that will create three different DataView from a static DataTable, which has three rows. You will see how three different views can be presented on GUI
public static DataView GetDataView(int viewType = 1) { DataView _resultView = null; DataTable _dtOriginal = DataTableExample.GetDT(); DataRow _newRow = _dtOriginal.NewRow(); _newRow["name"] = "WebTrainingRoom"; _newRow["mobile"] = "9852145363"; _newRow["city"] = "Kolkata"; _dtOriginal.Rows.Add(_newRow); _newRow.AcceptChanges(); // default view of datatable after adding new row DataView _defaultView = _dtOriginal.DefaultView; // filter out all original rows DataView firstView = new DataView(_dtOriginal); firstView.RowStateFilter = DataViewRowState.OriginalRows; // filter out all modified and added rows DataView secondView = new DataView(_dtOriginal); secondView.RowStateFilter = DataViewRowState.ModifiedCurrent | DataViewRowState.Added; switch (viewType) { case 1: _resultView = _defaultView; break; case 2: _resultView = firstView; break; case 3: _resultView = secondView; break; } return _resultView; }
Now calling the above function and binding the data with three different gridview on my asppx page
gvStudents.DataSource = DataViewExample.GetDataView(); gvStudents.DataBind(); GridView1.DataSource = DataViewExample.GetDataView(2); GridView1.DataBind(); GridView2.DataSource = DataViewExample.GetDataView(3); GridView2.DataBind();
This is how three different DataView will be presented to user from same DataTable, learn how to use GridView in Asp.net in case you don't know.
Notice
DataView secondView = new DataView(_dtOriginal); secondView.RowStateFilter = DataViewRowState.ModifiedCurrent | DataViewRowState.Added;
AddNew()
Delete(index)
Find(key)