locked
ListView ID Numbers RRS feed

  • Question

  • Hello to everyone,

    I have made a "program" where you can add items to listview and automatically a id is added:

    ID: Added automatically

    Name and Room: User select by another form

    but when I remove for example "Item2" ID of course dont change:

    I want automatically when item2 has ID: 1 and then autom. item5's ID change to the previous number("1")

    also not only to item5 and item6 that I will add later and item7 and 8.

    I add the ID with Listview.Count: listView1.Items.Count.ToString()


    George Andredakis

    The limits of what a computer can do are difined only from programmer's imagination

    Thursday, May 25, 2017 10:22 AM

Answers

  • Hi Ghex_AJ,

    Thank you for posting here.

    According to your question, you could try the following code.

          private void Form1_Load(object sender, EventArgs e)
            {
    
                listView1.View = View.Details;
                listView1.GridLines = true;
                listView1.FullRowSelect = true;
    
                //Add column header
                listView1.Columns.Add("ID", 100);
                listView1.Columns.Add("Name", 70);
                listView1.Columns.Add("Room", 70);
    
                //Add items in the listview
                string[] arr = new string[4];
                ListViewItem itm;
    
                //Add first item
                arr[0] = "0";
                arr[1] = "Item1";
                arr[2] = "Room1";
                itm = new ListViewItem(arr);
                listView1.Items.Add(itm);
    
                //Add second item
                arr[0] = "1";
                arr[1] = "Item2";
                arr[2] = "Room5";
                itm = new ListViewItem(arr);
                listView1.Items.Add(itm);
    
                //Add second item
                arr[0] = "2";
                arr[1] = "Item5";
                arr[2] = "Room1";
                itm = new ListViewItem(arr);
                listView1.Items.Add(itm);
    
    
            }
    
            //remove
            private void button1_Click(object sender, EventArgs e)
            {
                for (int i = listView1.Items.Count - 1; i >= 0; i--)
                {
                    if (listView1.Items[i].Selected)
                    {
                        listView1.Items[i].Remove();
                    }
                }
            }
    
            //refresh
            private void button2_Click(object sender, EventArgs e)
            {
                for (int i = listView1.Items.Count - 1; i >= 0; i--)
                {
                    listView1.Items[i].SubItems[0].Text = i.ToString();
                }
            }

    I hope this would be helpful.

    Best Regards,

    Wendy


    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.

    Friday, May 26, 2017 3:36 AM

All replies

  • Hello George Andredakis,

    You could update the ID's all records in the list when an item is removed.


    PS: Kindly close threads by marking answers where appropriate. Thank you.

    Thursday, May 25, 2017 12:13 PM
  • Yes but how I will do that

    George Andredakis

    The limits of what a computer can do are difined only from programmer's imagination

    Thursday, May 25, 2017 6:08 PM
  • Hi Ghex_AJ,

    Thank you for posting here.

    According to your question, you could try the following code.

          private void Form1_Load(object sender, EventArgs e)
            {
    
                listView1.View = View.Details;
                listView1.GridLines = true;
                listView1.FullRowSelect = true;
    
                //Add column header
                listView1.Columns.Add("ID", 100);
                listView1.Columns.Add("Name", 70);
                listView1.Columns.Add("Room", 70);
    
                //Add items in the listview
                string[] arr = new string[4];
                ListViewItem itm;
    
                //Add first item
                arr[0] = "0";
                arr[1] = "Item1";
                arr[2] = "Room1";
                itm = new ListViewItem(arr);
                listView1.Items.Add(itm);
    
                //Add second item
                arr[0] = "1";
                arr[1] = "Item2";
                arr[2] = "Room5";
                itm = new ListViewItem(arr);
                listView1.Items.Add(itm);
    
                //Add second item
                arr[0] = "2";
                arr[1] = "Item5";
                arr[2] = "Room1";
                itm = new ListViewItem(arr);
                listView1.Items.Add(itm);
    
    
            }
    
            //remove
            private void button1_Click(object sender, EventArgs e)
            {
                for (int i = listView1.Items.Count - 1; i >= 0; i--)
                {
                    if (listView1.Items[i].Selected)
                    {
                        listView1.Items[i].Remove();
                    }
                }
            }
    
            //refresh
            private void button2_Click(object sender, EventArgs e)
            {
                for (int i = listView1.Items.Count - 1; i >= 0; i--)
                {
                    listView1.Items[i].SubItems[0].Text = i.ToString();
                }
            }

    I hope this would be helpful.

    Best Regards,

    Wendy


    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.

    Friday, May 26, 2017 3:36 AM
  • Thanks it helps

    George Andredakis

    The limits of what a computer can do are difined only from programmer's imagination

    Saturday, May 27, 2017 7:26 AM