Solved

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

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 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