DataView in Ado.Net Example

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.

What is DataView in Ado.Net

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

Ado.net DataView Example

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.

dataview example in adonet

Notice

  1. First dataview: showing original data + newly added data to the same table.
  2. Second DataView: filter out all original rows, so only newly added row(s) will remain
  3. Third DataView has only the original rows, filter out all modified and newly added rows
This is how to use RowStateFilter in DataView to filter data; you can set multiple DataViewRowState value with pipe separation.
DataView secondView = new DataView(_dtOriginal);
secondView.RowStateFilter = DataViewRowState.ModifiedCurrent | DataViewRowState.Added;

ADO.net DataView Methods
  • AddNew()
    Add a new row in DataView object.
  • Delete(index)
    Delete specified index row from DataView instance.
  • Find(key)
    find a row with matching key value in DataView object
 
DataView in Ado.Net
Learn MS-SQL Development
Ado.net Interview Questions Answers
Connect SQL database using Ado.Net component from Asp.net application, learn how to use Ado.net as data access layer.
Ado.Net C# Examples | Join Asp.Net MVC Course