Click here to Skip to main content
15,891,900 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
user will give data in textbox to search [input is customer name] ., once data present in listview the entire row should get another color [default row color white]. i tried below coding, but in this case if customer name present in same name twice only one row getting color change [another row which same name not changing]


C#
for(int k=0;k<listView1.Items.Count;k++)
                    {

                        ListViewItem find_name = listView1.FindItemWithText(textBox1.Text);

                        if(find_name!=null)
                        {
                            //MessageBox.Show(find_name.ToString());
                            //show_booked_details();
                            find_name.BackColor = Color.DarkViolet;
                        }
                        else
                        {
                            MessageBox.Show("No Records Found");
                            textBox1.Text = "";
                            find_name.BackColor = Color.White;
                            //show_booked_details();
                        }

                    }
Posted

how do you call the function? What event is raised to call the function?

if the function is called when the box is changed; your code should work I guess. If you need to press a button to update the field i'd use this code:

C#
foreach (ListViewItem in listView1)
{
    ListViewItem find_name = listView1.FindItemWithText(textBox1.Text);
 
    if(find_name!=null)
    {
        //MessageBox.Show(find_name.ToString());
        //show_booked_details();
        find_name.BackColor = Color.DarkViolet;
    }
    else
    {
         MessageBox.Show("No Records Found");
         textBox1.Text = "";
         find_name.BackColor = Color.White;
         //show_booked_details();
    }

}
 
Share this answer
 
Comments
Umapathi K 1-Oct-12 6:09am    
i written in button click event
Umapathi K 1-Oct-12 6:30am    
foreach (ListViewItem in listView1)

error in this line
pieterjann 2-Oct-12 6:29am    
What does the error say? There should be something like that that works (didn't check sorry)
In your sample the search always begins at the first item, so it will only ever find the first occurrence. You need to use this version[^] of the FindItemWithText() method, to start the search at the item after the one that you previously found. You also need to optimise your loop so that it does not repeat for every item in the ListView.
 
Share this answer
 

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