Click here to Skip to main content
15,850,750 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hello guys,
First of all sorry for my bad english.
Is there any way how to calculate in listwiev?

I have a list view with two columns, ID and Number, using searching from text box.
foreach(ListViewItem ID in listView1.Items)

                if (search.Text == ID.Text)


ListViewItem ID;
                    ListViewItem.ListViewSubItem Num;
                    ID = new ListViewItem();
                    ID.Text = textBox1.Text;

                    Num = new ListViewItem.ListViewSubItem();
                    Num.Text = textBox2.Text;

Every ID have Specific number(column)

If i found mathes by ID how i can calculate with the number on the same row.
For example
ID: Alex , Number: 100
ID: John, Number: 50

If i type Alex to text box how i can calculate with Alex number?
Thanks so much
Updated 5-Jan-14 17:50pm

You can easily search the number within the listbox.
Have a look at this[^] example.
Share this answer
Here's a simple illustration of how to search a ListView for a ListViewItem that matches a string, and how to get the match's SubItem, test to see if it is convertible to a number, and convert it to a number.
private void button1_Click(object sender, EventArgs e)
    ListViewItem foundItem = listView1.FindItemWithText(textBox1.Text, false, 0, false);

    // found a match ?
    if (foundItem != null)
        // get the match's first sub-item
        string numberCandidate = foundItem.SubItems[1].Text;

        int idCandidate;

        // do we have an integer ?
        if (Int32.TryParse(numberCandidate, out idCandidate))
            // there's now a valid integer in 'idCandidate
            MessageBox.Show("ID #" + idCandidate.ToString());
If you focus on understanding what happens in this code, you should be able to adapt it to meet your needs.

Note: 'FindItemText with only a string parameter supplied will return "partial matches:" if you searched on "Al, and your ListView had Items titled "Alex," and "Alice," it would return whichever entry came first in the ListViewItems collection. In the code above we supply the optional argument (last argument) to 'FindItemText that "turns off" finding partial-matches.

Note: 'FindItemText will ignore whether your search string is upper- or lower- case.
Share this answer
SukyCZ 6-Jan-14 11:07am    
Thanks, that solved my problem!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900