15,850,750 members
5.00/5 (1 vote)
See more:
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.
SEARCH BUTTON
```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
Posted
Updated 5-Jan-14 17:50pm
v3

## Solution 1

You can easily search the number within the listbox.
Have a look at this[^] example.

## Solution 3

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.
C#
```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.