Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# How do you create a new DataTable column expression using a column name and a variable?

Posted on 2006-07-20
Medium Priority
767 Views
How do you create a new DataTable column expression using a columnName * variableName?

The following gives creates a new column, and an expression of MyColumn * (a fixed value)

' Create the new column.
Dim MyNewColumn As New DataColumn
MyNewColumn.ColumnName = "MyColumnName"
MyNewColumn.Expression = "AnotherColumnName * 7"

In the above example, I am after the 7 being a variable, not a predetermined fixed value.
0
Question by:IvanHowarth
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 6

Expert Comment

ID: 17145585
0

LVL 19

Accepted Solution

arif_eqbal earned 2000 total points
ID: 17145718
Dim i As Integer = 7
MyNewColumn.Expression = "AnotherColumnName * " & i

0

LVL 6

Expert Comment

ID: 17145758

Try this.,

Dim x As Integer = 9  // assign the value that you want to multiply with the column here

Dim ds As New DataSet
Dim dt As New DataTable
Dim dr As DataRow

Dim column1 As New DataColumn
column1.ColumnName = "Number"
column1.DataType = GetType(Integer)

Dim column2 As New DataColumn
column2.ColumnName = "MinValue"
column2.DataType = GetType(Integer)

Dim column3 As New DataColumn
column3.ColumnName = "TotalValue"
column3.DataType = GetType(Integer)
column3.Expression = String.Format("Number * {0}", x)

dr = dt.NewRow
dr.item(0) = 5
dr.item(1) = 10

dr = dt.NewRow
dr.item(0) = 20
dr.item(1) = 30

Thanks,
Nanda
0

Author Comment

ID: 17145772
arif_eqbal  answer was exactly what I needed.

Thanks both anyway.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
###### Suggested Courses
Course of the Month10 days, 6 hours left to enroll