Junior ADO.NET

What is the difference between a DataTable and a DataView?

Both DataTable and DataView are used to represent data in ADO.NET, but they have

different purposes:

  • DataTable: Represents a single table of data in-memory. It is a container for rows

and columns, and it is used to hold data returned from the database.

Key Features:

Follow:

  • Can hold data from one table.
  • Provides methods for data manipulation (e.g., adding, deleting, modifying

rows).

  • Can be filled with data from a DataAdapter.

Example:

DataTable table = new DataTable();

table.Columns.Add("ID");

table.Columns.Add("Name");

table.Rows.Add(1, "Alice");

table.Rows.Add(2, "Bob");

  • DataView: Provides a way to view and filter data from a DataTable without changing

the underlying data. It allows you to sort and filter the data dynamically.

Key Features:

  • Acts as a view of a DataTable.
  • Allows for sorting, filtering, and searching the data.
  • It does not modify the underlying DataTable.

Example:

DataView view = new DataView(table);

view.RowFilter = "Name = 'Alice'";

view.Sort = "ID DESC";

Summary:

  • DataTable holds data, and DataView provides a dynamic view (filtering, sorting) of

the DataTable.

Follow:

More from ADO.NET Core Tutorial

All questions for this course