Solved

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

Posted on 2006-07-20
4
761 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

728 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