What is the difference between a DataTable and a DataView?
Short answer: 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.
Explain a bit more
It is a container for rows and columns, and it is used to hold data returned from the database. Key Features: 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);
Example code
view.RowFilter = "Name = 'Alice'";
view.Sort = "ID DESC"; Summary: DataTable holds data, and DataView provides a dynamic view (filtering, sorting) of the DataTable.
Say this in the interview
- Define — one clear sentence (the short answer above).
- Example — relate it to a project like ShopNest or your real work.
- Trade-off — when you would not use it.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png