OK, i made a listView and a ComboBox
now i want that when ever i choose an item from the comboBox the listView will show only the items that there's subItem have the same text of the ComboBoxItem

this is what i made so far, but i succeeded to do it only with Remove and now with "Hide"
how can i do it?

string value = toolStripComboBox1.Text;
            for (int i = ListView1.Items.Count - 1; -1 < i; i--)
            {
                if
                (ListView1.Items[i].SubItems[1].Text.StartsWith(value) == false)
                {
                    ListView1.Items[i].Remove();
                }
            }

From where do you get data?

when ever i choose an item from the comboBox the listView will show only the items that there's subItem have the same text of the ComboBoxItem

Would you explain this a bit better? I dont have an idea of what you thing? Simply...

i hope you'll understand this better:

i made two objects, the first is a ListView i call it "listView1" and the second is a comboBox i call it "toolStripComboBox1".

------------------------

i have few items in this toolStripComboBox1.
i have items that there's first subItem have the same text of one item from the combobox.

------------------------

now i want that whenever i choose an item from the toolStripComboBox1 the listView will show only the items that there's subitems have the same item as the comboBox has.


i call it "ListView filter" it's like an a search,
or for example it's like a "Suggest & Append" in the properties.. but you can't do it on listView only on ComboBox or something like that.

Would you mind showing me an example? I have no clue about what you are talking about.
What subItems in comboBox? There is no subItems in comboBox.
I need an example code, with items and subItems.

this is the code: try the program yourself, and i want it's will hide the items and not remove them

I have finally understood what do you want, as soon as I have seen the form designer.
This is what I did for you:

namespace Apr23Test1
{
    public partial class Form1 : Form
    {
        List<MyClass> list;
        public Form1()
        {
            InitializeComponent();

            //creating listView:
            listView1.Columns.Add("Item", 80, HorizontalAlignment.Left);
            listView1.Columns.Add("SubItem 1", 80, HorizontalAlignment.Left);
            listView1.Columns.Add("SubItem 2", -2, HorizontalAlignment.Center);
            listView1.View = View.Details;
            listView1.FullRowSelect = true;

            //create a new instance of a list<T>:
            list = new List<MyClass>();

            //populate list<T>:
            AddingRows();

            //populating listView:
            ShowDataInListView();

            //creating comobBox`s event:
            this.comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
        }

        private void AddingRows()
        {
            //some example population wtih 6 letters:
            string letters = "ABACADC";
            for (int i = 0; i < letters.Length; i++)
            {
                if (!comboBox1.Items.Contains(letters[i].ToString()))
                    comboBox1.Items.Add(letters[i].ToString());
                MyClass mc = new MyClass();
                mc.Item = "Item " + (i + 1);
                mc.SubItem1 = "SubItem " + (i + 1);
                mc.SubItem2 = letters[i].ToString();
                list.Add(mc);
            }
        }

        private void ShowDataInListView()
        {
            foreach (MyClass mc in list)
            {
                ListViewItem lvi = new ListViewItem(mc.Item);
                lvi.SubItems.Add(mc.SubItem1);
                lvi.SubItems.Add(mc.SubItem2);
                listView1.Items.Add(lvi);
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string comboSelection = (string)comboBox1.SelectedItem;
            listView1.Items.Clear();
            foreach (MyClass mc in list)
            {
                if (comboSelection == mc.SubItem2)
                {
                    ListViewItem lvi = new ListViewItem(mc.Item);
                    lvi.SubItems.Add(mc.SubItem1);
                    lvi.SubItems.Add(mc.SubItem2);
                    listView1.Items.Add(lvi);
                }
            }
        }
    }

    class MyClass
    {
        public string Item { get; set; }
        public string SubItem1 { get; set; }
        public string SubItem2 { get; set; }
    }
}

Will add some code to add and remove the items...

I did the whole code. There are now 2 forms. 1st one is like yours, and 2nd one is meant to add items.
Ok, here is a whole code:

//Form1:
    public partial class Form1 : Form
    {
        List<MyClass> list;
        public Form1()
        {
            InitializeComponent();

            //creating listView:
            listView1.Columns.Add("Item", 80, HorizontalAlignment.Left);
            listView1.Columns.Add("SubItem 1", 80, HorizontalAlignment.Left);
            listView1.Columns.Add("SubItem 2", -2, HorizontalAlignment.Center);
            listView1.View = View.Details;
            listView1.FullRowSelect = true;

            //create a new instance of a list<T>:
            list = new List<MyClass>();

            //populate list<T> (example code):
            AddingRows();

            //populating listView:
            ShowDataInListView();

            //creating comobBox`s event:
            this.comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
            //some other event for adding and removing:
            this.buttonAdd.Click += new EventHandler(buttonAdd_Click);
            this.buttonRemove.Click += new EventHandler(buttonRemove_Click);
        }

        //EXAMPE CODE:
        private void AddingRows()
        {
            //some example population wtih 6 letters:
            string letters = "ABACADC";

            comboBox1.Items.Add("Show all");
            for (int i = 0; i < letters.Length; i++)
            {
                if (!comboBox1.Items.Contains(letters[i].ToString()))
                    comboBox1.Items.Add(letters[i].ToString());
                MyClass mc = new MyClass();
                mc.Item = "Item " + (i + 1);
                mc.SubItem1 = "SubItem " + (i + 1);
                mc.SubItem2 = letters[i].ToString();
                list.Add(mc);
            }            
        }

        //YOUR CODE TO ADD ROWS LATER ON:
        public void AddingRowsToList(string[] data)
        {
            //adding to list<T>:
            MyClass mc = new MyClass();
            mc.Item = data[0];
            mc.SubItem1 = data[1];
            mc.SubItem2 = data[2];
            list.Add(mc);

            //adding to comboBox if not exsits yet:
            if (!comboBox1.Items.Contains(data[2]))
                comboBox1.Items.Add(data[2]);

            //showing new data:
            ShowDataInListView();
        }

        private void ShowDataInListView()
        {
            listView1.Items.Clear();

            foreach (MyClass mc in list)
            {
                ListViewItem lvi = new ListViewItem(mc.Item);
                lvi.SubItems.Add(mc.SubItem1);
                lvi.SubItems.Add(mc.SubItem2);
                listView1.Items.Add(lvi);
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string comboSelection = (string)comboBox1.SelectedItem;
            listView1.Items.Clear();
            foreach (MyClass mc in list)
            {
                if (comboSelection == mc.SubItem2 || comboSelection == "Show all")
                {
                    ListViewItem lvi = new ListViewItem(mc.Item);
                    lvi.SubItems.Add(mc.SubItem1);
                    lvi.SubItems.Add(mc.SubItem2);
                    listView1.Items.Add(lvi);
                }
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2(this);
            f2.Show();
        }

        private void buttonRemove_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < listView1.SelectedIndices.Count; i++)
            {
                string _Item = listView1.SelectedItems[i].Text;
                string _SubItem1 = listView1.SelectedItems[i].SubItems[1].Text;
                string _SubItem2 = listView1.SelectedItems[i].SubItems[2].Text;
                for (int j = 0; j < list.Count; j++)
                {
                    if (list[j].Item == _Item &&
                        list[j].SubItem1 == _SubItem1 &&
                        list[j].SubItem2 == _SubItem2)
                    {
                        //remove from list<T>:
                        list.Remove(list[j]);
                        //show new data:
                        ShowDataInListView();
                        break;
                    }
                }
            }
        }
    }

    class MyClass
    {
        public string Item { get; set; }
        public string SubItem1 { get; set; }
        public string SubItem2 { get; set; }
    }

//
//Form2
//
 public partial class Form2 : Form
    {
        Form1 f1;
        public Form2(Form1 _f1)
        {
            InitializeComponent();
            this.button1.Click += new EventHandler(button1_Click);
            this.f1 = _f1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != String.Empty &&
              textBox2.Text != String.Empty &&
               textBox3.Text != String.Empty)
            {
                f1.AddingRowsToList(new string[] { textBox1.Text, textBox2.Text, textBox3.Text.ToUpper() }); //remove ToUpper() method if you dont need big letter only !!
                this.Dispose();
            }
            else
                MessageBox.Show("All fields are required.");          
        }
    }

HERE you can get the whole downloadable project. I hope this is what you have wanted it. I put some effort into, but I like to help.
Let me know what do you think,
cya around.

Member Avatar for Unhnd_Exception

Heres another example.

After populating the initial listviewitems create a copy of them.

When the combobx selected index changes clear the list view and then search in the array copy and add back what you need.

public partial class Form1 : Form
    {
        Object[] ListViewItemCopy ;
        
        public Form1()
        {
            InitializeComponent();

            comboBox1.Items.Add("ABC");
            comboBox1.Items.Add("ABD");
            comboBox1.Items.Add("ABF");

            listView1.Items.Add("A");
            listView1.Items[listView1.Items.Count - 1].SubItems.Add("ABC");
            listView1.Items.Add("B");
            listView1.Items[listView1.Items.Count - 1].SubItems.Add("ABD");
            listView1.Items.Add("C");
            listView1.Items[listView1.Items.Count - 1].SubItems.Add("ABC");

            ListViewItemCopy = new Object[listView1.Items.Count];
            listView1.Items.CopyTo(ListViewItemCopy, 0);

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == -1) return;

            listView1.BeginUpdate();

            listView1.Items.Clear();
            ListViewItem LI;
                
            for (int i = 0; i < ListViewItemCopy.Length; i++)
            {
                LI = (ListViewItem)ListViewItemCopy[i];
                if (LI.SubItems[1].Text == (String)comboBox1.SelectedItem)
                {
                    listView1.Items.Add(LI);
                }
            }
            listView1.EndUpdate();

        }
    }

wooooooooooow!!!!! Mitja Bonca you are the best!!! thank you very much!!! i don't know how did you have the power to write this all and also with examples and explains. thank you very much!! i am really appreciate this.

by the way.. hh 1 more question, i am load the items from a XML file, so i can just delete that items that you added, change some coding and it should work?

yes!!! never mind!! it's work!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! thank you dude!~!!!!! you are the best, you are the only one that help me in every thread, !!! thank!!!!

Solved!!

:)
You are welcome. Why I had to work? Hmm, you didnt let me step off, so I had to contine working. (btw, you can add a vote for me :) ).

If there is anything you dont know, come here, we`ll surely help you out.

I made it as Solved and i vote in ^ in some posts :) thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.