CodeSnip: Changing Data in a DataTable Using AcceptChanges Method
page 1 of 1
Published: 30 May 2006
Unedited - Community Contributed
Abstract
In this article, Brett describes how to use the AcceptChanges method of the DataRow class.
by Brett Burridge
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 16147/ 21

This short article describes how to use the AcceptChanges method of the DataRow class.  The method is invaluable if you need to change data in an ADO.NET DataTable object before displaying it in the web browser.  The procedure described here works regardless of where the data in the DataTable object was populated from.  In this particular example, the data is modified in a DataTable that is populated from static data, but it can just as easily be used to change data derived from a database.

Creating the data source

In this particular example, the DataTable containing the data to be modified is created and populated within code, but the technique could also be applied to a DataTable populated from a database.

This VB.NET code is used to create the DataTable object called MyDataTable, and populate it with four data columns.

Listing 1

'Create a datatable
Dim MyDataTable As NewDataTable("MyDataTable")
MyDataTable.Columns.Add(New DataColumn("UserID",System.Type.GetType("System.Int32")))
MyDataTable.Columns.Add(NewDataColumn("FirstName",System.Type.GetType("System.String")))
MyDataTable.Columns.Add(NewDataColumn("LastName",System.Type.GetType("System.String")))
MyDataTable.Columns.Add(New DataColumn("PhoneNumber",System.Type.GetType("System.String")))

Once the DataTable object has been created and its columns have been defined, data rows can be added to the DataTable.  In the example code below, a new row is added to the DataTable object MyDataTable.

Listing 2

'Populate the datatable with a data row
Dim DataRow1 As DataRow = MyDataTable.NewRow()
DataRow1("UserID"= 1
DataRow1("FirstName"= "Fred"
DataRow1("LastName"= "Jones"
DataRow1("PhoneNumber"= "5542"
MyDataTable.Rows.Add(DataRow1)

Once the DataTable object has been instantiated and populated with data, the object can be iterated.  In the VB.NET sample code below, the row containing the value of "1" for the UserID column has the value of the PhoneNumber column changed to "5643."  Calling the AcceptChanges() method on the current DataRow object then causes this change to be made to the row in the MyDataTable DataTable.

Listing 3

'Iterate through datatable
Dim MyDataRow As DataRow
For Each MyDataRow In MyDataTable.Rows
If Convert.ToInt32(MyDataRow("UserID")) =1 Then
MyDataRow("PhoneNumber"="5643"
MyDataRow.AcceptChanges()
End If
Next

The updated data can then be displayed in a DataGrid control in the usual way.

Listing 4

'Bind datatable to datagrid
DataGrid1.DataSource = MyDataTable
DataGrid1.DataBind()


User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-19 5:35:24 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search