DataView Class And Its Importance In .NET

Introduction

 
The DataView class is used to provide a customizable view of DataTable to sort, filter, search, edit, and navigate through the records. To sort and filter records, the RowFilter and Sort properties of the class must be set.
 

Properties and Methods of DataView Class

 
ApplyDefaultSort
 
This property specifies or retrieves a value indicating whether the default sort can be used.
 
FindRows
 
This method returns an array of DataRowView objects having columns with values that match the specified sort key value.
 
GetEnumerator
 
This method retrieves an enumerator for the current DataView.
 
The following SourceCode demonstrate how to filter records using the DataView class.
  1. SqlDataAdapter sqldaStudents=new SqlDataAdapter(“Select * from students”,sqlconStudents);  
  2. DataTable dtableStudents=new DataTable(“Students”);  
  3. sqldaStudents.Fill(dtableStudents);  
  4. DataRow drowStudents=dtableStudents.NewRow();  
  5. drowStudents[“StudentNo”]=1001;  
  6. drowStudents[“StudentName”]=”Ashish Bhatnagar”;  
  7. drowStudents[“Marks”]=89;  
  8. dtableStudents.Rows.Add(drowStudents);  
  9. drowStudents=dtableStudents.NewRow();  
  10. drowStudents[“StudentNo”]=1002;  
  11. drowStudents[“StudentName”]=”Rahul Kumar”;  
  12. drowStudents[“Marks”]=92;  
  13. dtableStudents.Rows.Add(drowStudents);  
  14. DataView dvwStudents=new DataView(dtableStudents);  
  15. dvwStudents.RowFilter=”Marks>65”;  
  16. for(int row=0;row<dvwStudents.Count;row++)  
  17. {  
  18.    MessageBox.Show(“Student No. :” + dvwStudents[row][“StudentNo”].ToString());  
  19.    MessageBox.Show(“Student Name. :” + dvwStudents[row][“StudentName”].ToString());  
  20. }  
In this source code, the DataTable object is filled with the records of the Students table. Two new rows are added to the table in the DataTable object using the DataRow class. An object of the DataView class is created and the class takes the DataTable object as the parameter in its constructor. The RowFilter property specifies the filter expression, which is set to Marks>65 . This will display only details of those students who have scored more than 65 marks. To accomplish this task , the for loop is used and the required student numbers and their corresponding names are retrieved.
 

DataViewManager Class

 
The DataViewManager class can be used in coordination with the DataViewSettingCollection class to change the view settings for tables in the dataset. It is used to fetch and manage data from multiple tables in the database.
 
A DataSet can store information from multiple tables or data sources. Due to this, it becomes necessary to apply proper view settings to individual tables in the DataSet. To achieve this task, the developer can use the DataViewManager class. The main purpose of DataViewmanager class is to ensure that a grid control displays each table accurately. This is necessary when the control is bound to a DataSet storing multiple tables.
 

Properties and Methods of DataViewManager class

 
DataViewSettingCollectionString
 
This property specifies or retrieves a value to be used for code persistence.
 
CreateDataView
 
This method creates a DataView for a particular table.
 
TableCollectionChnaged
 
This method raises the CollectionChnaged event when a DataTable is inserted or deleted from the DataTableCollection.
 
DataGrid Class
 
The DataGrid class is used to implement the DataGrid control. The control is bound only to a single instance of the DataSet class. The DataGrid control is used to display a single table or related tables in a hierarchical manner. The class defines some useful properties to customize the look of the control
 

Properties of DataGrid Class

 
AllowNavigation
 
Specifies or retrieves a value indicating whether user navigation is allowed.
 
BackColor
 
Specifies or retrieves the background color of even-numbered rows in the grid.
 
BorderStyle
 
Specifies or retrieves the border style of the grid.
 
CaptionText
 
Specifies or retrieves the text appearing as the caption on the grid.
 
FirstVisibleColumn
 
Retrieves the index of the first visible column in the grid.
 
FlatMode
 
It retrieves or specifies a value indicating whether the grid displays in flat mode.
 
GridLineColor
 
Specifies or retrieves the color of the grid lines.
 
TableStyles
 
It retrieves a collection of DataGridTableStyle objects for the grid.
 

Events of DataGrid class

 
BorderStyleChanged
 
Occurs when the values of the BorderStyle property have been changed.
 
CurrentCellChanged
 
Occurs when the value of the CurrentCell property has been changed.
 
CursorChanged
 
Occurs when the value of the cursor property has been changed.
 
DataSourceChanged
 
Occurs when the value of the DataSource property has been changed.
 
Navigate
 
Occurs when the user has the navigated to a new table.
 
Source Code demonstrate to retrieve the row number of the focused cell using the DataGrid class
  1. DataGrid dgridOrders;  
  2. SqlDataAdapter sqldaOrders=new SqlDataAdapter(“Select * from Order”,sqlconOrders);  
  3. DataSet dsetOrders=new DataSet(“Orders”);  
  4. sqldaOrders.Fill(dsetOrders,”Orders”);  
  5. dgridOrders=new DataGrid();  
  6. dgridOrders.CaptionText=”Order Details”;  
  7. dgridOrders.DataSource=dsetOrders;  
  8. dgridOrders.DataMember=”Orders”;  
  9. dgridOrder.Size=new Size(600,300);  
  10. ……..  
  11. private void dgridOrder_CurrentCellChanged(object sender, EventArgs e)  
  12. {  
  13.    lblRecordNumber.Text=Convert.ToString(dgridOrders.CurrentCell.RowNumber +1);  
  14. }  
In this source code, the DataSet object is filled with the records of the Orders table. An instance of the DataGrid class is created. The CaptionText property specifies the caption text of the grid as Order Details. The DataSource property is used to specify the DataSet as the data source for the grid. This means that the grid control will fetch the records from a particular table in the DataSet.
 
The DataMember property specifies the table whose records will be displayed in the control. The width and height of the control are set to 600 and 300 respectively using the Size property. When the user focuses on any cell in the grid, the CurrentCellChanged event is raised. When this event is raised, the row number of the focused cell is displayed in a Label control.
 

Summary

 
The DataView class is used to provide a customizable view of DataTable to sort, filter, search, edit, and navigate through the records. The DataGrid control is used to display a single table or related tables in a hierarchical manner. The DataSource property is used to specify the DataSet as the data source for the grid.