How to create a DataView

The DataView in ADO.NET provides versatile views of the data stored in a DataTable, enabling various operations such as sorting, filtering, searching, and modifying the data. These capabilities make it a powerful tool for managing and presenting data in different ways.

DataTable

With a DataView, you can customize how the data is displayed and manipulated without directly modifying the underlying DataTable. This allows for dynamic and flexible data presentation based on specific criteria or user requirements. By using the sorting feature, you can arrange the data in ascending or descending order based on one or more columns. Filtering allows you to selectively display rows that meet specific criteria, making it easier to focus on relevant data. Searching enables you to locate specific values within the DataView. Additionally, you can add new rows to the underlying DataTable and modify existing rows directly through the DataView, with any changes automatically reflected in the DataTable.

DataViews can be created and configured both at design time and at runtime, depending on the requirements of your application. You have two options for creating a DataView. First, you can use the DataView constructor, which allows you to create a DataView instance and specify various parameters such as the source DataTable, filter criteria, sort criteria, and row state filter. This gives you fine-grained control over the DataView's configuration. Alternatively, you can create a reference to the DefaultView property of the DataTable, which provides a default DataView that exposes the entire content of the DataTable. This is a simpler option if you don't need to apply specific filters or sorting.

dataView = dataSet.Tables(0).DefaultView

The following source code shows how to create a DataView in VB.NET. Create a new VB.NET project and drag a DataGridView and a Button on default Form Form1 , and copy and paste the following Source Code on button click event.

Full Source VB.NET
Imports System.Data.SqlClient Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim connetionString As String Dim connection As SqlConnection Dim command As SqlCommand Dim adapter As New SqlDataAdapter Dim ds As New DataSet Dim dv As DataView Dim sql As String connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password" sql = "Select * from product" connection = New SqlConnection(connetionString) Try connection.Open() command = New SqlCommand(sql, connection) adapter.SelectCommand = command adapter.Fill(ds, "Create DataView") adapter.Dispose() command.Dispose() connection.Close() dv = ds.Tables(0).DefaultView DataGridView1.DataSource = dv Catch ex As Exception MsgBox(ex.ToString) End Try End Sub End Class

Conclusion

It's important to note that changes made to a DataView are automatically reflected in the underlying DataTable. Similarly, modifications made to the DataTable will be immediately visible in any DataView objects that are viewing the DataTable. This ensures data consistency and synchronization between the DataView and the DataTable.