Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

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

License

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

Share

About the Author

kevininstructor
Instructor / Trainer
United States United States
Trouble shooter, component builder, and architect using Microsoft Visual Studio 2012. 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).

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140905.1 | Last Updated 22 Sep 2010
Article Copyright 2010 by kevininstructor
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid