Solved

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

Posted on 2006-07-20
4
755 Views
Last Modified: 2008-01-09
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"

        ' Add column to DataTable
        Me.MyDataSet.MyDataTable.Columns.Add(MyNewColumn)
     
In the above example, I am after the 7 being a variable, not a predetermined fixed value.
0
Comment
Question by:IvanHowarth
  • 2
4 Comments
 
LVL 6

Expert Comment

by:Nandakumar Sakthivel
ID: 17145585
0
 
LVL 19

Accepted Solution

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


0
 
LVL 6

Expert Comment

by:Nandakumar Sakthivel
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)
dt.Columns.Add(column1)

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

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

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

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

Thanks,
Nanda
0
 

Author Comment

by:IvanHowarth
ID: 17145772
arif_eqbal  answer was exactly what I needed.

Thanks both anyway.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now