Wednesday 31 July 2013

What is DataView in asp.net with Example? What are the advantages?


A DataView provides various views of the data stored in a DataTable. Using a DataView, you can expose the data in a table with different sort orders, and you can filter the data by row state or based on a filter expression.

DataView Example:

public void BindGridDataAll()
    {
        DataSet ds = new DataSet();
       //Get data from database and fill into dataset        
        ds = ObjClass.GetData();
        dvData = ds.Tables[0].DefaultView;
        // Here we specify which column you want to sort
         dvData.Sort = "SortString";
         GridView.DataSource = dvData;
         GridView.DataBind();
    }


Advantages of DataView:

1.      The main advantage that I could think of is to be able to filter and sort the data without having to hit the database server. To be exact, they are useful for user interfaces in which you allow users to sort and filter a set of data but don't want to have to keep re-querying the database.

2.      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. Using a DataView, you can expose the data in a table with different sort orders, and you can filter the data by row state or based on a filter expression.

3.      A DataView provides a dynamic view of data whose content, ordering, and membership reflect changes to the underlying DataTable as they occur. This is different from the Select method of the DataTable, which returns a DataRow array from a table per a particular filter and/or sort order and whose content reflects changes to the underlying table, but whose membership and ordering remain static. The dynamic capabilities of the DataView make it ideal for data-binding applications.

4.      A DataView provides you with a dynamic view of a single set of data to which you can apply different sorting and filtering criteria, similar to the view provided by a database. However, a DataView differs significantly from a database view in that the DataView cannot be treated as a table and cannot provide a view of joined tables. You also cannot exclude columns that exist in the source table, nor can you append columns, such as computational columns, that do not exist in the source table.

No comments:

Post a Comment