Sum on a listview

I've the following code to populate a listview with records from my database:

Private Sub preencherListView()
Dim lstItem As ListViewItem

Call conexao()
sSQL = "SELECT * FROM [TempoTotalFamiliaQuery] WHERE (Data LIKE'" & dtpDataRelatorio.Text & "')"
da = New OleDb.OleDbDataAdapter(sSQL, con)
da.Fill(ds, "TempoTotalFamiliaQuery")

For i As Integer = 0 To ds.Tables("TempoTotalFamiliaQuery").Rows.Count - 1
lstItem = New ListViewItem()
lstItem.Text = (ds.Tables("TempoTotalFamiliaQuery").Rows(i).Item(0))
lstItem.SubItems.Add(ds.Tables("TempoTotalFamiliaQuery").Rows(i).Item(1))
lstFamilias.Items.Add(lstItem)
Next
lstFamilias.EndUpdate()
con.Close()
End Sub

lstFamilias.Items.Clear()
lstFamilias.BeginUpdate()
With Me.lstFamilias
.GridLines = True
.MultiSelect = False
.FullRowSelect = True
.View = View.Details
.HideSelection = False
.Columns.Add("Familia", 140, HorizontalAlignment.Left)
.Columns.Add("Tempo", 50, HorizontalAlignment.Center)
End With
Call preencherListView()

How can I calculate the sum of one of the fields that is showned on the listview as column "Tempo"

I've tryed to do something like this:

For i As Integer = 0 To ds.Tables("TempoTotalFamiliaQuery").Rows.Count - 1
lstItem = New ListViewItem()
lstItem.Text = (ds.Tables("TempoTotalFamiliaQuery").Rows(i).Item(0))
lstItem.SubItems.Add(ds.Tables("TempoTotalFamiliaQuery").Rows(i).Item(1))
lstFamilias.Items.Add(lstItem)
sumTotal=sumTotal+ds.Tables("TempoTotalFamiliaQuery").Rows(i).Item(1)
Next

but this way nothing is showned on the listview!

Can annyone help me please

Thanks,

Joao Pinto



Answer this question

Sum on a listview

  • BioGeek

    That's what the problem is, the data on the field/column is of time type, in other words is like "00:12", "01:04", etc. I want to have a time total on a variable. I've tryed Int32 type but I get a convertion error.

    Joao Pinto


  • japt

    Thanks again for your answer.

    The code you provided it's not working also. Maybe the problem is that column "Tempo" as time values and when I try to sum them I get a conversion error.

    What is the best way to sum a time field from a dataset

    Thanks,

    Joao Pinto


  • Mateus1223

    what error do you get Remember you need to be sure that the values of the field/column are of same types (integer in this case) otherwise you will get the conversion error.

  • jamiec

    no one said its impossible :-) What doesnt work exactly you need to explain :-)

    from what I understand, you wish to sum up the total time values yes

    Dim theTotalTimeValues as new TimeSpan()

    For i As Integer = 0 To ds.Tables("TempoTotalFamiliaQuery").Rows.Count - 1
    lstItem = New ListViewItem()
    lstItem.Text = (ds.Tables("TempoTotalFamiliaQuery").Rows(i).Item(0))
    lstItem.SubItems.Add(ds.Tables("TempoTotalFamiliaQuery").Rows(i).Item(1))
    lstFamilias.Items.Add(lstItem)

    theTotalTimeValues.Add(TimeSpan.Parse(ds.Tables("TempoTotalFamiliaQuery").Rows(i).Item(1).ToString())
    Next

    this will total up the time values from the specified columns on each row you iterate through.



  • Khurram Javaid

    its because you have not told it to add anything in the list view! :-)

    lstFamilias.Items.Add(sumTotal)

    does this work



  • AndyL

    What I want to do is to sum a column from the listview! I don't want to add the sumTotal to the listview. I want sumTotal to be the sum of column "Tempo" of the listview.

    How can I do that

    Thanks,

    Joao Pinto


  • Chuck Turner

    you may wish to use a TimeSpan in this case. The thing that is currently bothering me is the colon in the middle of the time.

    http://msdn2.microsoft.com/en-us/library/system.timespan.aspx

    you could use TimeSpan.Parse(value) to see if it can parse the value to a TimeSpan then add this to the total timespan, this should work OK. Example:

    Dim theTime as String = "00:12"

    Dim totalSpan as TimeSpan = TimeSpan.Parse(theTime)

    Dim anotherTime as String = "00:30"

    totalSpan = totalSpan.Add(TimeSpan.Parse(anotherTime)) 'will add anotherTime to the totalSpan instance



  • chakravarthy_b

    It's not working also!

    How can I add time values I have values like "00:30" (30 minutes), "01:10" (one hour and 10 minutes), etc! Is it possible to add them I don't believe that it's impossible in VB!!!

    Joao Pinto


  • search and deploy

    well going with the code:

    For i As Integer = 0 To ds.Tables("TempoTotalFamiliaQuery").Rows.Count - 1
                lstItem = New ListViewItem()
                lstItem.Text = (ds.Tables("TempoTotalFamiliaQuery").Rows(i).Item(0))
                lstItem.SubItems.Add(ds.Tables("TempoTotalFamiliaQuery").Rows(i).Item(1))
                lstFamilias.Items.Add(lstItem)
    sumTotal=sumTotal+ds.Tables("TempoTotalFamiliaQuery").Rows(i).Item(1)
            Next

     

    Should be fine. Whats the problem you may need 1 alteration:

    sumTotal=sumTotal+Convert.ToInt32(ds.Tables("TempoTotalFamiliaQuery").Rows(i).Item(1))



  • Sum on a listview