none
How to search item from ListView RRS feed

  • Question

  • I have a textbox and a listview.

    How to find item of which a second column is equal to the string in the textbox?

    Sunday, June 16, 2019 3:27 AM

Answers

  • Hello,

    Perhaps like this where X is the column to search.

    foreach (ListViewItem item in listView.Items)
    {
        if (item.SubItems[X].Text == someTextBox.Text)
        {
             // TODO
        }
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Jeff0803 Monday, June 17, 2019 3:33 AM
    Sunday, June 16, 2019 10:32 AM
    Moderator
  • In Karen's reply, I think she meant to actually put a number for the index instead of an X.

    // Since you want to compare to the 2nd column, use 1 for the index
    if (item.SubItems[1].Text == someTextBox.Text)
    



    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Marked as answer by Jeff0803 Monday, June 17, 2019 3:34 AM
    Sunday, June 16, 2019 8:29 PM
    Moderator
  • Hi Jeff0803,

    Thank you for posting here.

    Based on your description, you want to find a item which a second column is equal to the string in the textbox.

    You could try the following code.

     private void Form1_Load(object sender, EventArgs e)
            {
                listView1.Items.Add("test1");
                listView1.Items.Add("test2");
                listView1.Items.Add("test3");
                listView1.Items.Add("test4");
                listView1.Items.Add("test1");
                listView1.Items.Add("test2");
                listView1.Items.Add("test5");
                listView1.Items.Add("test6");
                listView1.Items.Add("test1");
                listView1.Items.Add("test2");
                listView1.Items.Add("test3");
            }
    
            private void Button1_Click(object sender, EventArgs e)
            {
                int count = 1;
                int m = 7;
                foreach (ListViewItem item in listView1.Items)
                {
                    if(count%m==2)
                    {
                        if(item.Text==textBox1.Text)
                        {
                            MessageBox.Show("Test");
                        }
                    }
                    count++;
                }
            }
    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Jeff0803 Monday, June 17, 2019 3:33 AM
    Monday, June 17, 2019 3:01 AM
    Moderator

All replies

  • Hello,

    Perhaps like this where X is the column to search.

    foreach (ListViewItem item in listView.Items)
    {
        if (item.SubItems[X].Text == someTextBox.Text)
        {
             // TODO
        }
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Jeff0803 Monday, June 17, 2019 3:33 AM
    Sunday, June 16, 2019 10:32 AM
    Moderator
  • In Karen's reply, I think she meant to actually put a number for the index instead of an X.

    // Since you want to compare to the 2nd column, use 1 for the index
    if (item.SubItems[1].Text == someTextBox.Text)
    



    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Marked as answer by Jeff0803 Monday, June 17, 2019 3:34 AM
    Sunday, June 16, 2019 8:29 PM
    Moderator
  • Hi Jeff0803,

    Thank you for posting here.

    Based on your description, you want to find a item which a second column is equal to the string in the textbox.

    You could try the following code.

     private void Form1_Load(object sender, EventArgs e)
            {
                listView1.Items.Add("test1");
                listView1.Items.Add("test2");
                listView1.Items.Add("test3");
                listView1.Items.Add("test4");
                listView1.Items.Add("test1");
                listView1.Items.Add("test2");
                listView1.Items.Add("test5");
                listView1.Items.Add("test6");
                listView1.Items.Add("test1");
                listView1.Items.Add("test2");
                listView1.Items.Add("test3");
            }
    
            private void Button1_Click(object sender, EventArgs e)
            {
                int count = 1;
                int m = 7;
                foreach (ListViewItem item in listView1.Items)
                {
                    if(count%m==2)
                    {
                        if(item.Text==textBox1.Text)
                        {
                            MessageBox.Show("Test");
                        }
                    }
                    count++;
                }
            }
    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Jeff0803 Monday, June 17, 2019 3:33 AM
    Monday, June 17, 2019 3:01 AM
    Moderator