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
            .Add(fractColumn)
        End With
    End Sub

Recommended Answers

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
     .Add(fractColumn)
 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
     .Add(fractColumn)
 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)

Try This instead:

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
     .Add(fractColumn)
    End With
Next

Thanks Begginnerdev for your reply.
But by putting the

 With dtOrder.Columns
         .Add(fractColumn)
        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
            dtOrder.Columns.Add(New DataColumn("Total", GetType(String)))
            For i = 0 To dtOrder.Rows.Count - 1
                If IsNumeric((dtOrder.Rows(i)(3).ToString)) Then
                    val = CDec(dtOrder.Rows(i)(3).ToString)
                    dtOrder.Rows.Add()
                    dtOrder.Rows(i).Item("Total") = ((dlgNewTop.Dec2Frac(val, 16)))
                End If
            Next
            dtOrder.Columns(dtOrder.Columns.Count - 1).SetOrdinal(3)
        End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.