---
title: "ADO.NET DataView Class"  
description: "ADO.NET DataView Class"  
author: "ICSM Computer"  
published: 2025-02-12  
updated: 2025-02-12  
canonical: https://www.mindstick.com/interview/33992/ado-dot-net-dataview-class  
category: "ado.net"  
tags: ["c#", "ado.net"]  
reading_time: 3 minutes  

---

# ADO.NET DataView Class

The `DataView` class in ADO.NET provides a way to represent a **customized** view of a `DataTable`. It allows sorting, filtering, and searching within a `DataTable` without modifying the original data.

## Namespace:

```plaintext
using System.Data;
```

#### Key Features of `DataView`

1. **Filtering**: Filter rows based on conditions using the `RowFilter` property.
2. **Sorting**: Sort data using the `Sort` property.
3. **Searching**: Use `Find()` to locate rows.
4. **Editing**: Modify rows without affecting the original `DataTable`.
5. **Dynamic Updates**: Reflects changes made to the `DataTable` dynamically.

**Creating a** `DataView`

```cs
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Age", typeof(int));

dt.Rows.Add(1, "Alice", 25);
dt.Rows.Add(2, "Bob", 30);
dt.Rows.Add(3, "Charlie", 22);

DataView dv = new DataView(dt);
```

**Sorting in** `DataView`

```cs
dv.Sort = "Age ASC";  // Sort by Age in ascending order
```

**Filtering in** `DataView`

```cs
dv.RowFilter = "Age > 25";  // Show only rows where Age > 25
```

**Finding Rows in** `DataView`

Before using `Find()`, the `Sort` property must be set.

```cs
dv.Sort = "ID";
int index = dv.Find(2);  // Find row where ID = 2

if (index != -1)
    Console.WriteLine("Row found: " + dv[index]["Name"]);
```

## Adding New Row

```cs
DataRowView newRow = dv.AddNew();
newRow["ID"] = 4;
newRow["Name"] = "David";
newRow["Age"] = 28;
newRow.EndEdit();
```

#### When to Use `DataView`?

- When you need **real-time filtering and sorting** without modifying the original `DataTable`.
- When working with **large datasets** where creating a new `DataTable` for every filter/sort would be inefficient.
- In **data-bound controls** like `GridView`, where you want to show a filtered view.

## Answers

### Answer by ICSM Computer

The `DataView` class in ADO.NET provides a way to represent a **customized** view of a `DataTable`. It allows sorting, filtering, and searching within a `DataTable` without modifying the original data.

## Namespace:

```plaintext
using System.Data;
```

#### Key Features of `DataView`

1. **Filtering**: Filter rows based on conditions using the `RowFilter` property.
2. **Sorting**: Sort data using the `Sort` property.
3. **Searching**: Use `Find()` to locate rows.
4. **Editing**: Modify rows without affecting the original `DataTable`.
5. **Dynamic Updates**: Reflects changes made to the `DataTable` dynamically.

**Creating a** `DataView`

```cs
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Age", typeof(int));

dt.Rows.Add(1, "Alice", 25);
dt.Rows.Add(2, "Bob", 30);
dt.Rows.Add(3, "Charlie", 22);

DataView dv = new DataView(dt);
```

**Sorting in** `DataView`

```cs
dv.Sort = "Age ASC";  // Sort by Age in ascending order
```

**Filtering in** `DataView`

```cs
dv.RowFilter = "Age > 25";  // Show only rows where Age > 25
```

**Finding Rows in** `DataView`

Before using `Find()`, the `Sort` property must be set.

```cs
dv.Sort = "ID";
int index = dv.Find(2);  // Find row where ID = 2

if (index != -1)
    Console.WriteLine("Row found: " + dv[index]["Name"]);
```

## Adding New Row

```cs
DataRowView newRow = dv.AddNew();
newRow["ID"] = 4;
newRow["Name"] = "David";
newRow["Age"] = 28;
newRow.EndEdit();
```

#### When to Use `DataView`?

- When you need **real-time filtering and sorting** without modifying the original `DataTable`.
- When working with **large datasets** where creating a new `DataTable` for every filter/sort would be inefficient.
- In **data-bound controls** like `GridView`, where you want to show a filtered view.


---

Original Source: https://www.mindstick.com/interview/33992/ado-dot-net-dataview-class

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
