Skip to content

Commit 3c8ea10

Browse files
committed
draft
1 parent 3728241 commit 3c8ea10

File tree

1 file changed

+40
-40
lines changed

1 file changed

+40
-40
lines changed

docs/data-tools/edit-data-in-datasets.md

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Edit data in datasets using the .NET Framework
33
description: Edit data in datasets with the .NET Framework and Visual Studio and insert new rows into a dataset, identify changed rows, and find rows with errors.
4-
ms.date: 11/04/2016
4+
ms.date: 04/24/2025
55
ms.topic: how-to
66
dev_langs:
77
- VB
@@ -13,21 +13,33 @@ author: ghogen
1313
ms.author: ghogen
1414
manager: mijacobs
1515
ms.subservice: data-tools
16+
#customer intent: As a developer, I want to know how to edit data in .NET Framework datasets so that I have more understanding of .NET Framework dataset technologies.
17+
1618
---
1719

1820
# Edit data in datasets using the .NET Framework
1921

2022
[!INCLUDE [Data access tech note](./includes/data-technology-note.md)]
2123

22-
You edit data in data tables much like you edit the data in a table in any database. The process can include inserting, updating, and deleting records in the table. In a data-bound form, you can specify which fields are user-editable. In those cases, the data-binding infrastructure handles all the change tracking so that the changes can be sent back to the database later. If you programmatically make edits to data, and you intend to send those changes back to the database, you must use the objects and methods that do the change tracking for you.
24+
This article describes how to query and edit data in tables in .NET Framework datasets. You can edit data in data tables much like you edit the tables in any database. You can insert, update, and delete records in the table. In a data-bound form, you can specify which fields are user-editable.
25+
26+
In those cases, the data-binding infrastructure handles all the change tracking so that the changes can be sent back to the database. If you edit data programmatically, and you intend to send the changes back to the database, you must use objects and methods that do the change tracking.
27+
28+
In addition to changing the actual data, you can also query a <xref:System.Data.DataTable> to return specific rows of data. For example, you can query for individual rows, original or proposed versions of rows, rows that changed, or rows that contain errors.
29+
30+
## Prerequisites
2331

24-
In addition to changing the actual data, you can also query a <xref:System.Data.DataTable> to return specific rows of data. For example, you might query for individual rows, specific versions of rows (original and proposed), rows that have changed, or rows that have errors.
32+
To use Visual Studio to query and edit data in .NET Framework datasets, you need:
2533

26-
## To edit rows in a dataset
34+
- The **.NET desktop development** and **Data storage and processing** workloads installed in Visual Studio. For more information, see [Modify Visual Studio](../install/modify-visual-studio.md).
35+
- A C# or Visual Basic (VB) .NET Framework project created.
36+
- A [dataset created](pass-data-between-forms.md#create-the-data-source) from a [SQL Server Express LocalDB](https://www.microsoft.com/sql-server/sql-server-downloads) database.
37+
38+
## Edit rows in a dataset
2739

2840
To edit an existing row in a <xref:System.Data.DataTable>, you need to locate the <xref:System.Data.DataRow> you want to edit, and then assign the updated values to the desired columns.
2941

30-
If you don't know the index of the row you want to edit, use the `FindBy` method to search by the primary key:
42+
If you don't know the index of the row you want to edit, use the <xref:System.Data.DataRowCollection.Find> method to search by the primary key.
3143

3244
### [C#](#tab/csharp)
3345

@@ -38,7 +50,7 @@ If you don't know the index of the row you want to edit, use the `FindBy` method
3850
:::code language="vb" source="../snippets/visualbasic/VS_Snippets_VBCSharp/VbRaddataEditing/VB/Form1.vb" id="Snippet3":::
3951
---
4052

41-
If you know the row index, you can access and edits rows as follows:
53+
Once you know the row index, you can use it to access and edit the row.
4254

4355
### [C#](#tab/csharp)
4456

@@ -49,12 +61,11 @@ If you know the row index, you can access and edits rows as follows:
4961
:::code language="vb" source="../snippets/visualbasic/VS_Snippets_VBCSharp/VbRaddataEditing/VB/Form1.vb" id="Snippet5":::
5062
---
5163

52-
## To insert new rows into a dataset
53-
54-
Applications that use data-bound controls typically add new records through the **Add New** button on a [BindingNavigator control](/dotnet/framework/winforms/controls/bindingnavigator-control-windows-forms).
64+
## Insert or delete new rows into a dataset
5565

56-
To manually add new records to a dataset, create a new data row by calling the method on the DataTable. Then, add the row to the <xref:System.Data.DataRow> collection (<xref:System.Data.DataTable.Rows%2A>) of the <xref:System.Data.DataTable>:
66+
Applications that use data-bound controls typically add new records by using the **Add New** button on a [BindingNavigator control](/dotnet/framework/winforms/controls/bindingnavigator-control-windows-forms).
5767

68+
You can also add a new record to a dataset by calling the <xref:System.Data.DataTable.NewRow%2A> method on the `DataTable`. Then, add the row to the <xref:System.Data.DataRow> collection(<xref:System.Data.DataTable.Rows%2A>).
5869
### [C#](#tab/csharp)
5970

6071
:::code language="csharp" source="../snippets/csharp/VS_Snippets_VBCSharp/VbRaddataEditing/CS/Form1.cs" id="Snippet1":::
@@ -64,18 +75,15 @@ To manually add new records to a dataset, create a new data row by calling the m
6475
:::code language="vb" source="../snippets/visualbasic/VS_Snippets_VBCSharp/VbRaddataEditing/VB/Form1.vb" id="Snippet1":::
6576
---
6677

67-
In order to retain the information that the dataset needs to send updates to the data source, use the <xref:System.Data.DataRow.Delete%2A> method to remove rows in a data table. For example, if your application uses a TableAdapter (or <xref:System.Data.Common.DataAdapter>), the TableAdapter's `Update` method deletes rows in the database that have a <xref:System.Data.DataRow.RowState%2A> of <xref:System.Data.DataRowState.Deleted>.
78+
### Delete records from a data table
6879

69-
If your application does not need to send updates back to a data source, it's possible to remove records by directly accessing the data row collection (<xref:System.Data.DataRowCollection.Remove%2A>).
80+
You can delete a <xref:System.Data.DataRow> from the <xref:System.Data.DataRowCollection> by calling the <xref:System.Data.DataRowCollection.Remove%2A> method of the <xref:System.Data.DataRowCollection>, or by calling the <xref:System.Data.DataRow.Delete%2A> method of the <xref:System.Data.DataRow> object.
7081

71-
#### To delete records from a data table
82+
The <xref:System.Data.DataRowCollection.Remove%2A> method removes the row from the collection. If your application doesn't need to send updates back to a data source, you can remove records by directly accessing the data row collection with <xref:System.Data.DataRowCollection.Remove%2A>.
7283

73-
- Call the <xref:System.Data.DataRow.Delete%2A> method of a <xref:System.Data.DataRow>.
84+
Conversely, the <xref:System.Data.DataRow.Delete%2A> method doesn't actually remove the <xref:System.Data.DataRow>, but marks it for deletion. The actual removal occurs when you call <xref:System.Data.DataRow.AcceptChanges%2A> method. You can programmatically check which rows are marked for removal before actually deleting them.
7485

75-
This method doesn't physically remove the record. Instead, it marks the record for deletion.
76-
77-
> [!NOTE]
78-
> If you get the count property of a <xref:System.Data.DataRowCollection>, the resulting count includes records that have been marked for deletion. To get an accurate count of records that aren't marked for deletion, you can loop through the collection looking at the <xref:System.Data.DataRow.RowState%2A> property of each record. (Records marked for deletion have a <xref:System.Data.DataRow.RowState%2A> of <xref:System.Data.DataRowState.Deleted>.) Alternatively, you can create a data view of a dataset that filters based on row state and get the count property from there.
86+
To retain the information that the dataset needs to send updates to the data source, use the <xref:System.Data.DataRow.Delete%2A> method to remove the <xref:System.Data.DataRow> from the data table. If your application uses a `TableAdapter` or <xref:System.Data.Common.DataAdapter>, the `Update` method deletes rows that have a `RowState` of `Deleted`.
7987

8088
The following example shows how to call the <xref:System.Data.DataRow.Delete%2A> method to mark the first row in the `Customers` table as deleted:
8189

@@ -88,27 +96,25 @@ The following example shows how to call the <xref:System.Data.DataRow.Delete%2A>
8896
:::code language="vb" source="../snippets/visualbasic/VS_Snippets_VBCSharp/VbRaddataEditing/VB/Form1.vb" id="Snippet8":::
8997
---
9098

91-
<a name='determine-if-there-are-changed-rows'></a>
99+
> [!NOTE]
100+
> If you get the count property of a <xref:System.Data.DataRowCollection>, the resulting count includes records that are marked for deletion. To get an accurate count of records that aren't marked for deletion, you can loop through the collection looking at the <xref:System.Data.DataRow.RowState%2A> property of each record. Records marked for deletion have a <xref:System.Data.DataRow.RowState%2A> of <xref:System.Data.DataRowState.Deleted>.
101+
>
102+
> Alternatively, you can create a data view of a dataset that filters based on row state and get the count property from it.
92103
104+
<a name='determine-if-there-are-changed-rows'></a>
93105
## Determine whether there are changed rows
94106

95-
When changes are made to records in a dataset, information about those changes is stored until you commit them. You commit the changes when you call the `AcceptChanges` method of a dataset or data table, or when you call the `Update` method of a TableAdapter or data adapter.
107+
When you make changes to records in a dataset, information about those changes is stored until you commit them. You commit the changes when you call the `AcceptChanges` method of a dataset or data table, or when you call the `Update` method of a `TableAdapter` or data adapter.
96108

97109
Changes are tracked two ways in each data row:
98110

99-
- Each data row contains information related to its <xref:System.Data.DataRow.RowState%2A> (for example, <xref:System.Data.DataRowState.Added>, <xref:System.Data.DataRowState.Modified>, <xref:System.Data.DataRowState.Deleted>, or <xref:System.Data.DataRowState.Unchanged>).
100-
101-
- Each changed data row contains multiple versions of that row (<xref:System.Data.DataRowVersion>), the original version (before changes) and the current version (after changes). During the period when a change is pending (the time when you can respond to the <xref:System.Data.DataTable.RowChanging> event), a third version — the proposed version— is available as well.
102-
103-
The <xref:System.Data.DataSet.HasChanges%2A> method of a dataset returns `true` if changes have been made in the dataset. After determining that changed rows exist, you can call the `GetChanges` method of a <xref:System.Data.DataSet> or <xref:System.Data.DataTable> to return a set of changed rows.
104-
105-
<a name='to-determine-if-changes-have-been-made-to-any-rows'></a>
111+
- Each data row contains information related to its <xref:System.Data.DataRow.RowState%2A>, for example <xref:System.Data.DataRowState.Added>, <xref:System.Data.DataRowState.Modified>, <xref:System.Data.DataRowState.Deleted>, or <xref:System.Data.DataRowState.Unchanged>.
106112

107-
#### To determine whether changes have been made to any rows
113+
- Each changed data row contains multiple versions. The <xref:System.Data.DataRowVersion> includes the original version before changes and the current version after changes. While a change is pending, you can respond to the <xref:System.Data.DataTable.RowChanging> event, and a third proposed version is also available.
108114

109-
- Call the <xref:System.Data.DataSet.HasChanges%2A> method of a dataset to check for changed rows.
115+
Call the <xref:System.Data.DataSet.HasChanges%2A> method of a dataset to check for changed rows. The method returns `true` if changes were made in the dataset. After determining that changes exist, you can call the `GetChanges` method of a <xref:System.Data.DataSet> or <xref:System.Data.DataTable> to return a set of changed rows.
110116

111-
The following example shows how to check the return value from the <xref:System.Data.DataSet.HasChanges%2A> method to detect whether there are any changed rows in a dataset named `NorthwindDataset1`:
117+
The following example shows how to check the return value from the <xref:System.Data.DataSet.HasChanges%2A> method to detect whether there are any changed rows in `NorthwindDataset1`.
112118

113119
### [C#](#tab/csharp)
114120

@@ -123,11 +129,7 @@ The following example shows how to check the return value from the <xref:System.
123129

124130
You can also check to see what type of changes were made in a dataset by passing a value from the <xref:System.Data.DataRowState> enumeration to the <xref:System.Data.DataSet.HasChanges%2A> method.
125131

126-
#### To determine what type of changes have been made to a row
127-
128-
- Pass a <xref:System.Data.DataRowState> value to the <xref:System.Data.DataSet.HasChanges%2A> method.
129-
130-
The following example shows how to check a dataset named `NorthwindDataset1` to determine whether any new rows have been added to it:
132+
The following example shows how to check the `NorthwindDataset1` dataset to determine whether any new rows were added.
131133

132134
### [C#](#tab/csharp)
133135

@@ -138,13 +140,11 @@ The following example shows how to check a dataset named `NorthwindDataset1` to
138140
:::code language="vb" source="../snippets/visualbasic/VS_Snippets_VBCSharp/VbRaddataEditing/VB/Form1.vb" id="Snippet13":::
139141
---
140142

141-
## To locate rows that have errors
142-
143-
When working with individual columns and rows of data, you might encounter errors. You can check the `HasErrors` property to determine whether errors exist in a <xref:System.Data.DataSet>, <xref:System.Data.DataTable>, or <xref:System.Data.DataRow>.
143+
## Locate rows that have errors
144144

145-
1. Check the `HasErrors` property to see whether there are any errors in the dataset.
145+
When you work with individual columns and rows of data, you might encounter errors. You can check the `HasErrors` property to determine whether errors exist in a <xref:System.Data.DataSet>, <xref:System.Data.DataTable>, or <xref:System.Data.DataRow>.
146146

147-
2. If the `HasErrors` property is `true`, iterate through the collections of tables, and then the through the rows, to find the row with the error.
147+
If the `HasErrors` property for the dataset is `true`, iterate through the collections of tables, and then through the rows, to find the rows with the errors.
148148

149149
### [C#](#tab/csharp)
150150

0 commit comments

Comments
 (0)