Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i'm using C# (windows form application)
I have a listview in my program like this
Id          Name          Quantity
001     AAA     111
002     BBB     222
003     CCC     333
004     DDD     444

i'm new to C# So i want to when i input name in textbox and click the search button then select the equal value in name column and show all data in my lables then i use this code to check listview column value but i need any idea to else part (select equal data sat and display in my lables)

EX - i search AAA then i want to my
lable1's value 001
lable2's value AAA
lable3's value 111

C#
foreach (ListViewItem item in listview1.Items)
    {
        if (item.SubItems[2].Text == name.Text)
        {
         // any idea to this part
        }
    }


Thank in advance
Posted
Comments
[no name] 16-Mar-13 17:04pm    
Did you try label1.Text = item.SubItems[1].Text or label3.Text = item.SubItems[3].Text?

There is a property for this:

Additional documentation information can be found here[^].

I modified your code sample a bit:
C#
int searchIndex = 0; //Use a counter to know on which row you currently are
foreach (ListViewItem item in listview1.Items)
    {
        if (item.SubItems[2].Text == name.Text)
        {//If text is found, select row at the current index
           listview1.Items[searchIndex].Selected = true;
           label1.Text = item.SubItems[0].Text;
           label2.Text = item.SubItems[1].Text;
           label3.Text = item.SubItems[2].Text;
        }
        else
        {//If text is not found, deselect row at the current index
            listview1.Items[searchIndex].Selected = false;
        }
        //afterwards, increase searchIndex ++1
        searchIndex++;
    }
 
Share this answer
 
v2
Comments
Michael Haephrati 16-Mar-13 18:09pm    
I have deleted my solution, as your solution is perfect. Thanks!
Marco Bertschi 16-Mar-13 18:42pm    
You are welcome, even when I overlooked a part of the question.

Cheers,
Marco Bertschi
Jegan Thiyagesan 16-Mar-13 18:27pm    
How does your solution assign values to the label as asked in the question?
What is the point of having that "searchIndex"?
Marco Bertschi 17-Mar-13 9:23am    
I have now added the assignement of the labels.
The searchIndex counts which row of the ListView we are currently searching, afterwards we can select the corresponding row at the searchIndex.

cheers,
Marco Bertschi
Jegan Thiyagesan 17-Mar-13 10:10am    
You do not need a variable to keep the index of selected row in listView. List view already has aselected items collection. Assuming that only one item is selected, getting that index will be:

if (listView1.SelectedItems.count > 0)
{
ListViewItem selectedItem = listView1.SelectedItems[0];
}

In the case like above, it should be using for loop instead of foreach.

for(int index = 0; index <listview1.Items.count; index++)
{
if (listview1.Items[index].SubItems[2].Text.Equals(name.Text))
{
//If text is found, select row at the current index
listview1.Items[index].Selected = true;
label1.Text = listview1.Items[index].SubItems[0].Text;
label2.Text = listview1.Items[index].SubItems[1].Text;
label3.Text = listview1.Items[index].SubItems[2].Text;
}
else
{
//If text is not found, deselect row at the current index
listview1.Items[index].Selected = false;
}
}
In your ListView "Name" Column is second Column so its index is 1.

C#
foreach (ListViewItem item in listView1.Items)
     {
         if (!string.IsNullOrEmpty(item.Text))//Check Item value is not Null or Empty
         {
             if (item.SubItems[1].Text == name.Text)
             {
                 label1.Text = item.SubItems[0].Text;
                 label2.Text = item.SubItems[1].Text;
                 label3.Text = item.SubItems[2].Text;
             }
         }

      }
 
Share this answer
 
v2
Comments
Manoj Chamikara 17-Mar-13 3:46am    
Thank You gajenp Appreciate your help
Marco Bertschi 17-Mar-13 9:26am    
Please do not write things like "If u got ur Answer pls mark it." into your solution.
It is very impolite and we do not like to see it on CP.
I removed the bit of begging now.

In addition, what do you do when the searched item is suddenly at index 0?

cheers,
Marco Bertschi

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