Search code examples
vb.netdatatableoperation

Multiply a Datacolumn by a parameter


I have a DataColumn and I want to multiply all the rows in it by a certain value. Assuming that I have one column named price, the DataColumn.Expression property allows me to do this in the following way:

MyDataColumn.Expression = "price * 0.0862"

But what if I do want to use a parameter? I would like to write:

Dim tax As Double = 0.0862
MyDataColumn.Expression = "price * tax"

But doing so in Visual Basic actually multiplies the column price with the column tax, which is not what I want.

Any ideas?


Solution

  • Why not just use a variable like this:

    Dim tax As Double = 0.0862
    MyDataColumn.Expression = "price * " & tax
    

    Edit:

    You have to convert tax to a string with InvariantCulture first:

    Dim tax As Double = 0.0862
    MyDataColumn.Expression = "price * " & tax.ToString(CultureInfo.InvariantCulture)
    

    since otherwise, MyDataColumn.Expression will be price * 0,0862 instead of price * 0.0862 and the DataColumn will complain about a bad syntax.