Hi all
I have a Products table which has a description and a size column the description is a string value and the size has a double value.
What I need to do is after reading the data it converts the decimals value of the size column into a fraction.
After the convertion I need to add a new column with the new fraction value onto the current datatable so I can populate the datagrid.
in the following code only the last value will be added on all the rows.

``````Private Sub ConvertDecimal2Fraction()
Dim val As Double
Dim fractColumn As New DataColumn
Dim rowcnt As Integer = (dtOrder.Rows.Count - 1)
For x = 0 To rowcnt
If IsNumeric((dtOrder.Rows(x)(3).ToString)) Then
val = CInt(dtOrder.Rows(x)(3).ToString)
With fractColumn
.ColumnName = "xttl"
.Expression = ((dlgNewTop.Dec2Frac(val, 2)))
End With
End If
Next
With dtOrder.Columns
End With
End Sub
``````

The code:

``````Dim rowcnt As Integer = (dtOrder.Rows.Count - 1)
For x = 0 To rowcnt
If IsNumeric((dtOrder.Rows(x)(3).ToString)) Then
val = CInt(dtOrder.Rows(x)(3).ToString)
With fractColumn
.ColumnName = "xttl"
.Expression = ((dlgNewTop.Dec2Frac(val, 2)))
End With
End If
Next

With dtOrder.Columns
End With
``````

Will only add the last …

## All 2 Replies

The code:

``````Dim rowcnt As Integer = (dtOrder.Rows.Count - 1)
For x = 0 To rowcnt
If IsNumeric((dtOrder.Rows(x)(3).ToString)) Then
val = CInt(dtOrder.Rows(x)(3).ToString)
With fractColumn
.ColumnName = "xttl"
.Expression = ((dlgNewTop.Dec2Frac(val, 2)))
End With
End If
Next

With dtOrder.Columns
End With
``````

Will only add the last because that is the only row you are telling it to add.

This code is outside the for:

`````` With dtOrder.Columns
End With
``````

Therefore, the only value added would be the last value it sees. (The value of the column from the last row of dtOrders)

``````For x = 0 to dtOrder.Rows.Count - 1
If IsNumeric((dtOrder.Rows(x)(3).ToString)) Then
val = CInt(dtOrder.Rows(x)(3).ToString)
With fractColumn
.ColumnName = "xttl"
.Expression = ((dlgNewTop.Dec2Frac(val, 2)))
End With
End If

With dtOrder.Columns
End With
Next
``````

But by putting the

`````` With dtOrder.Columns
End With
``````

Inside of the for next loop I getan Error "Column "xttl" already belongs to this datatale "
But it works Using this Code

`````` Private Sub ConvertDecimal2Fraction()
Dim val As Decimal