0

I have a listview with subitems and in one of the subs I need to make the value displayed a currency. The code I am using errors with 'conversion from string "N2" to type 'integer' is not valid'. I know I need to format the sub, but I am not sure how to do this.

I would be grateful if someone could point out the right way to code this. Many thanks

Sub filllistview()
        Try
            'creatconn()
            cn.Open()
            Dim cmd As OleDbCommand = New OleDbCommand("Select * from Postings", cn)
            dr = cmd.ExecuteReader()
            While dr.Read()

                ListView1.Items.Add(dr(4).ToString())
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(11).ToString())
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(7).ToString())
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(5).ToString())
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(14).ToString())
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(3).ToString("N2")) <--- **This is the subitem**

            End While

        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        Finally
            dr.Close()
            cn.Close()
        End Try
    End Sub

1 Answer 1

1

dr(3) returns the Object type. Cast it to integer first:

ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(CInt(dr(3)).ToString("C"))
1
  • I need that to be currency. It seems that is converting to int. How do I display currency, like £23.56 Thanks Nov 17, 2013 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.