Click here to Skip to main content
13,000,982 members (61,294 online)
Click here to Skip to main content
Add your own
alternative version


3 bookmarked
Posted 22 Sep 2010

VS2010/VB.NET update single column for a rows in a DataTable

, 22 Sep 2010
Rate this:
Please Sign up or sign in to vote.
The following code for VS2010 shows how to update a single column for all rows in a DataTable by enumerating through the DataTable as an array using Array.ForEach method.

<br />
<pre lang="vb"><br />
<br />
Private Sub UpdateSingleColumnInAllRows()<br />
    Dim dtTable As New DataTable<br />
<br />
    With dtTable.Columns<br />
        .AddRange(New DataColumn() _<br />
        {<br />
              New DataColumn("ID", System.Type.GetType("System.String")),<br />
              New DataColumn("FirstName", System.Type.GetType("System.String")),<br />
              New DataColumn("LastName", System.Type.GetType("System.String"))<br />
        }<br />
    )<br />
    End With<br />
<br />
    dtTable.Rows.Add(New Object() {"100", "Kevin", "Gallagher"})<br />
    dtTable.Rows.Add(New Object() {"200", "Katrina", "Gallagher"})<br />
    dtTable.Rows.Add(New Object() {"300", "Zack", "Gallagher"})<br />
<br />
    dtTable.AsEnumerable.ToList.ForEach(<br />
        Sub(row) Console.WriteLine("{0} {1}",<br />
                                   row.Item("FirstName"), row.Item("LastName")))<br />
<br />
    Console.WriteLine()<br />
<br />
    Array.ForEach(dtTable.AsEnumerable.ToArray,<br />
        Sub(row As DataRow) row("Lastname") = row("LastName").ToString.ToUpper)<br />
<br />
    dtTable.AsEnumerable.ToList.ForEach(<br />
        Sub(row) Console.WriteLine("{0} {1}",<br />
                                   row.Item("FirstName"), row.Item("LastName")))<br />
<br />
End Sub<br />

In the above code all last names (Gallagher) are updated to (GALLAGHER)

Note that in the Action of the Array.ForEach you need to cast the type of the param to DataRow, otherwise this code will not work.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Instructor / Trainer
United States United States
Microsoft MVP, Trouble shooter, component builder, and architect using Microsoft Visual Studio 2012/2013. Prior to VS .NET I developed solutions using Borland Delphi and before Delphi CA Clipper. I enjoy assisting others, sharing knowledge and learning from others. Outside of IT my passions are playing guitar and teaching self-defense. Currently a moderator for Microsoft Social forums in C#, VB.NET and Data binding forums. Lastly a MCC (Microsoft Community Contributor).

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170624.1 | Last Updated 22 Sep 2010
Article Copyright 2010 by karenpayne
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid