Solved

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

Posted on 2006-07-20
4
762 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
[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
  • Learn & ask questions
  • 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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

626 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