hello everyone...

can you please help me with the project that im building... my project has a combobox with the items of january-december... a listview with a 14 column. in the first column is name, the second is year and the rest is the month starting from january to december, at the last column is the total of the column in january to december. i can display all the data in listview to mysql.. my problem is i wanted to select a certain month from the columns and display the total of that column in a label... i hope you understand me... thanks so much

Dani AI

Generated

— two practical ways to do this and a few troubleshooting tips so the month selection always shows the correct total.

Client-side (fast to implement): find the ListView column whose header text matches the ComboBox selection, then loop the ListViewItems and sum that SubItem. The snippet below handles missing subitems and common number formats (commas, currency symbols):

' VB.NET — sum selected month from a Details-mode ListView
Dim monthName As String = ComboBox1.SelectedItem?.ToString()
If String.IsNullOrEmpty(monthName) Then
    LabelTotal.Text = "0.00" : Return
End If

Dim colIndex As Integer = -1
For i As Integer = 0 To ListView1.Columns.Count - 1
    If String.Equals(ListView1.Columns(i).Text, monthName, StringComparison.OrdinalIgnoreCase) Then
        colIndex = i : Exit For
    End If
Next
If colIndex = -1 Then LabelTotal.Text = "0.00" : Return

Dim total As Decimal = 0D
For Each it As ListViewItem In ListView1.Items
    If it.SubItems.Count > colIndex Then
        Dim raw As String = it.SubItems(colIndex).Text.Trim()
        Dim value As Decimal
        If Decimal.TryParse(raw, Globalization.NumberStyles.Number Or Globalization.NumberStyles.AllowCurrencySymbol, Globalization.CultureInfo.CurrentCulture, value) Then
            total += value
        Else
            Dim cleaned = System.Text.RegularExpressions.Regex.Replace(raw, "[^0-9\.\-]", "")
            Decimal.TryParse(cleaned, Globalization.NumberStyles.Number, Globalization.CultureInfo.InvariantCulture, value)
            total += value
        End If
    End If
Next
LabelTotal.Text = total.ToString("N2")

Server-side (recommended for large datasets): run a MySQL SUM on the month column. Keep column names whitelisted (do not insert raw user text into SQL) and use parameterized queries for filters like year:

  • Maintain an array of allowed month column names and pick by ComboBox index.
  • Query: SELECT SUM(MonthColumn) FROM table WHERE year = @yr

Troubleshooting / tips:

  • If old rows “merge” when you refresh the ListView, call ListView1.BeginUpdate(), ListView1.Items.Clear(), repopulate, then ListView1.EndUpdate().
  • If duplicates appear in the database, check your INSERT logic and primary/unique keys (or use INSERT ... ON DUPLICATE KEY UPDATE intentionally).
  • If you need filtering, sorting, and summaries often, consider binding a DataTable to a DataGridView — it makes these operations easier than manual ListView manipulation.

its like, the previous data being inserted will just merge from the new inserted one.

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.