How to create a DataView | C#

The DataView functionality offers a versatile and powerful means of obtaining distinct perspectives on the data stored within a DataTable. Leveraging a DataView, developers can perform a range of operations, including sorting, filtering, and searching within a DataTable, in addition to the ability to add new rows and modify existing content. DataViews can be created and configured both at design time and runtime, providing flexibility in adapting to evolving data requirements.

Notably, any modifications made to a DataView automatically reflect in the underlying DataTable, ensuring seamless synchronization. Similarly, changes made to the underlying DataTable directly impact any DataView objects that are currently viewing the DataTable. This bidirectional relationship between DataViews and DataTables facilitates efficient data management and ensures consistency across different views of the data.

Creating a DataView

Creating a DataView can be achieved through two distinct approaches. Firstly, developers can utilize the DataView Constructor, which allows for the creation of a DataView object either with an empty constructor, or by providing a DataTable as a single argument. Additionally, the constructor can accept additional parameters such as filter criteria, sort criteria, and a row state filter, enabling more fine-grained control over the DataView's configuration. Alternatively, developers can establish a reference to the DefaultView Property of the DataTable, accessing the default DataView associated with the DataTable.

dv = ds.Tables[0].DefaultView;

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

Full Source C#
using System; using System.Data; using System.Data.SqlClient; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string connetionString = null; SqlConnection connection ; SqlCommand command ; SqlDataAdapter adapter = new SqlDataAdapter(); DataSet ds = new DataSet(); DataView dv ; string sql = null; 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 (Exception ex) { MessageBox.Show (ex.ToString()); } } } }


The DataView functionality serves as a crucial tool for manipulating and presenting data stored within a DataTable. Through its dynamic capabilities, developers can harness the power of sorting, filtering, and searching, facilitating enhanced data exploration and analysis. By creating and configuring DataViews, developers can adapt data representations to specific requirements, enabling effective data visualization and retrieval in various contexts.