Hi,
So here's what I have, Ive got a Listbox which displays a data/record from a SQL database, it displays "Amount" and only numbers would appear. it has a datasource.

Goal: I'd Like to get the total sum of those numbers from the Listbox and Display it to a textbox automatically or by button click event.

I thought of two ways to do this, but I don't have a clue on what to do.

1. I tried summing it all up on that exact Listbox and display the total on the textbox, here's the code that i came up with..

Dim sum As Double
        For x As Integer = 0 To ListBox1.Items.Count - 1
            sum += CDbl(ListBox1.Items(x))
        Next
        TotalTextBox.Text = sum.ToString

But the data im working with is from a database, so i got this error:

Conversion from type 'DataRowView' to type 'Double' is not valid.


2. I thought of having another Listbox and "Copy" the contents shown on the Listbox1 and then sum it all up on the Listbox2, then lastly display the Total on the Textbox. I did a little research on how to accomplish that but all the clue i found is that i have to disconnect the listbox data from the datasource from being bound, and reconnect it again.. I really don't have any idea on how i could do that with an SQL database.

Here's a quick flow on what im trying to accomplish to make things clear for you guys.

Record from Database -----Displays on---> Listbox1 -----User Clicks--> Button1 ----Displays Total sum on----> Textbox1

If you haven't disposed of the data source you could always use that to calculate the total. You can even do this before the button is pressed and store the total for later use.
Or this would probably work:

sum += CDbl(ListBox1.Items(x).Value) // or maybe .Text

You were getting the error because your code was returning a row view and not the value in the row (my VB.net is a little used these day so I maybe wrong).

commented: For wrong solution, Also error in code... -1

thanks for the quick reply, after i did that i got this error:

Public member 'Value' on type 'DataRowView' not found.

Man, Im totally stuck on this one..

Here is your problem..

Dim sum As Double
        For x As Integer = 0 To ListBox1.Items.Count - 1
            sum += CDbl(ListBox1.Items(x))'Here actually problem occurred
        Next
        TotalTextBox.Text = sum.ToString

Problem occurred because u r trying to convert a collection into string..

Use this code

Dim sum As Double
        For x As Integer = 0 To ListBox1.Items.Count - 1
            sum += Val(ListBox1.Items.Item(x).ToString)
        Next
        TotalTextBox.Text = sum.ToString

Hope this will help you....

commented: very informative replies! +1
commented: this is exactly what i needed, worked perfectly! i put this code in and everything works great, you actually just saved me from repeating year 11 :D +0

Thanks for the Informative reply! but the values aren't really adding up, it always sums up to a zero.

if you are binding your listbox with data source so you can do the sum like this also

Dim i As Integer = 0

For Each _row As DataRow In DirectCast(listBox1.DataSource, DataTable).Rows
	i += Convert.ToInt32(_row(1))
Next
MessageBox.Show(i.ToString())

DirectCast(listBox1.DataSource, DataTable).Rows convert your listbox to DataTable
and it loop on each Row. where _row(1) this is my column which contains amount
so you can write a index but better way is write your Column name insted of Index.

Gawd, I wish I have the powers to make the codes myself! man should have taken this course in a better school..

thanks! but i got this error..

Unable to cast object of type 'System.Windows.Forms.BindingSource' to type 'System.Data.DataTable'.

Edit: man im really sorry if i can't help much in solving this problem, Im trying to learn vb.net through google and reading ebooks. but it doesn't really help much when you're on time pressure. thanks for all the effort I really do appreciate it.

see the below zip file which is only example where i have done this without any error.

Can you please post the whole code here instead? or maybe a private message or somewhere. 'cause I got an error trying to open it up.
Btw, Im using vb.net 2008 express.

hello !
you can do it like in this way

Dim total As Int32
        Dim i As Integer
        For i = 0 To ListBox1.Items.Count - 1
            total = total + Val(ListBox1.Items(i))
        Next
        txttotal.Text = total

hope this will solve you prob , if yes then vote me up :P
Regards

I know this is not a good code but it will work for you...

try this

Dim sum As Double = 0
        'ListBox1.Items.Item(i).ToString()
        For i As Integer = 0 To Me.ListBox1.Items.Count - 1
            ListBox1.SelectedIndex = i
            sum += Val(ListBox1.SelectedValue.ToString)

        Next
MsgBox(sum.ToString)

if you are binding your listbox with data source so you can do the sum like this also

Dim i As Integer = 0

For Each _row As DataRow In DirectCast(listBox1.DataSource, DataTable).Rows
	i += Convert.ToInt32(_row(1))
Next
MessageBox.Show(i.ToString())

DirectCast(listBox1.DataSource, DataTable).Rows convert your listbox to DataTable
and it loop on each Row. where _row(1) this is my column which contains amount
so you can write a index but better way is write your Column name insted of Index.

Correct me if Im wrong, so if in my database the Column that contains the "Amount" is named "Amount" then the code should look like this in my button?

Dim i As Integer = 0

        For Each Amount As DataRow In DirectCast(ListBox5.DataSource, DataTable).Rows
            i += Convert.ToInt32(Amount(1))
        Next
        MessageBox.Show(i.ToString())
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.