MrzJkl
MrzJkl

Reputation: 341

Combine values from two DataTable columns into a new column

I have a DataSet which contains a DataTable with columns named FirstName, LastName and ComboName. I want to assign the the following value to the ComboName column in the same row.

Dim ComboName As String = LastName & ", " & FirstName

I don't want to do this by setting the value of the column manually, because I want the value of the ComboName column to automatically update when the first or last name is changed.

I was trying to do it with the DataTable.TableNewRow event, but how can I access the values of the specific "new row"?

Upvotes: 2

Views: 937

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460038

You can add a calculated column with an expression(scroll to syntax):

table.Columns.Add("ComboName", GetType(string), "LastName + ', ' + FirstName")

Sample:

Dim table as New DataTable 
table.Columns.Add("LastName")
table.Columns.Add("FirstName")
table.Columns.Add("ComboName", GetType(string), "LastName + ', ' + FirstName")
table.Rows.Add("Schmelter", "Tim")

Now the ComboName column of the single row automatically got: Schmelter, Tim

Upvotes: 4

Related Questions