user6609676
user6609676

Reputation:

C# - Update Values of row in Listview

I've a listview with the drone's ip, current position and desired position. My only problem is that when current position or desired position are updated, it adds a new drone with the same ip. I just want to update the values of current position and desired position for that particular ip.

enter image description here

            ip_drone_master = this.master.ip_dron();
            ip_drone_slave = this.slave.ip_dron();

            PointF posicao_master = master.pos_atual();
            posicao_atual_master = posicao_master;

            PointF posicao_slave = slave.pos_atual();
            posicao_atual_slave = posicao_slave;

            pos_atual_master = " X: " + posicao_atual_master.X + " Y: " + posicao_atual_master.Y;
            pos_atual_slave = " X: " + posicao_atual_slave.X + " Y: " + posicao_atual_slave.Y;

            pos_desej_master = " X:  " + posicao_desejada_master.X + " Y:  " + posicao_desejada_master.Y;
            pos_desej_slave = " X:  " + posicao_desejada_slave.X + " Y:  " + posicao_desejada_slave.Y;

            ListViewItem item_master = lv_lista_posicoes.FindItemWithText(pos_atual_master);
            ListViewItem item_slave = lv_lista_posicoes.FindItemWithText(pos_atual_slave);

            ListViewItem item_master_desej = lv_lista_posicoes.FindItemWithText(pos_desej_master);
            ListViewItem item_slave_desej = lv_lista_posicoes.FindItemWithText(pos_desej_slave);


            if (item_master != null && item_master_desej != null)
            {

            }
            else
            {

                ListViewItem lv = new ListViewItem(ip_drone_master);
                lv.SubItems.Add(pos_atual_master);
                lv.SubItems.Add(pos_desej_master);                 
                lv_lista_posicoes.Items.Add(lv);

                chart1.Series[0].Points.Clear();
                chart1.Series[0].Points.AddXY(posicao_atual_master.X, posicao_atual_master.Y);

            }


            if (item_slave != null && item_slave_desej != null)
            {

            }
            else
            {                   
                ListViewItem lv2 = new ListViewItem(ip_drone_slave);
                lv2.SubItems.Add(pos_atual_slave);
                lv2.SubItems.Add(pos_desej_slave);
                lv_lista_posicoes.Items.Add(lv2);

                chart1.Series[1].Points.Clear();
                chart1.Series[1].Points.AddXY(posicao_atual_slave.X, posicao_atual_slave.Y);
            }

Upvotes: 1

Views: 1890

Answers (1)

technikfischer
technikfischer

Reputation: 146

If I see this correctly, you are just adding new items with

 lv_lista_posicoes.Items.Add(lv);

but are never removing them. But nevertheless, replacing the item if you only want to change the content is a bad way. You can just keep track of the single elements (or search them by the IP), and only change the content by calling

lv2.SubItems[0].Text = pos_atual_slave;
lv2.SubItems[1].Text = pos_desej_slave;

where lv2 is the ListViewItem with the IP you want to update

Also see this answer

Upvotes: 1

Related Questions