Results 1 to 2 of 2

Thread: [RESOLVED] sum listbox items

Threaded View

  1. #1

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Resolved [RESOLVED] sum listbox items

    Iam adding a elemnet from a xml file into listbox
    Code:
    Option Strict On
    Option Explicit On
    Option Infer Off
    Imports System.IO
    Imports System.Xml
    Imports System.Net
    Imports System.Windows.Forms.VisualStyles.VisualStyleElement.ListView
    
    Public Class Form1
        Dim path As String = ("F:\ESS_Contacts\dsContactsSUBS.xml")
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim ds As New DataSet
            ds.ReadXml(path)
            ListBox1.DataSource = ds.Tables("Contacts")
            ListBox1.DisplayMember = "amount"
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim totall As Double
            For Each item As Double In ListBox1.Items
                totall += item
            Next
            txtTotalCost.Text = CType(totall, String)
    
        End Sub
    End Class
    I am then trying to sum the list but keeping getting errors

    System.InvalidCastException: 'Conversion from type 'DataRowView' to type 'Double' is not valid.'

    I have worked out its because of the way i add the data

    Ive tried heaps of examples but always get the same error.

    ps. ignore images, seems to be no way to delete once uploaded...
    Attached Images Attached Images   

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width