locked
change inserted item in listview RRS feed

Answers

  • Hi, my code is an example code that has 3 columns in listView (firstname, lastname and age). It shows how to update selected rows.

    The new form (form2) opens on DOUBLE CLICK of the row, it show the data that  are currently in the listview, which you can alter and when you press button all new data are inserted (updated) in listview - of previously selected item!

    Do it this way:

    //FORM 1:
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                listView1.Columns.Add("First name", 100, HorizontalAlignment.Left);
                listView1.Columns.Add("Last name", 100, HorizontalAlignment.Left);
                listView1.Columns.Add("Age", -2, HorizontalAlignment.Left);
                listView1.View = View.Details;
                listView1.FullRowSelect = true;
    
                //adding some example data:
                listView1.Items.Add(new ListViewItem(new string[] { "first 1", "last 1", "20" }));
                listView1.Items.Add(new ListViewItem(new string[] { "first 2", "last 2", "22" }));
                listView1.Items.Add(new ListViewItem(new string[] { "first 3", "last 3", "18" }));
                listView1.Items.Add(new ListViewItem(new string[] { "first 4", "last 4", "25" }));
            }
    
            private void listView1_DoubleClick(object sender, EventArgs e)
            {
                Person p = new Person();
                p.First = listView1.SelectedItems[0].Text;
                p.Last = listView1.SelectedItems[0].SubItems[1].Text;
                p.Age = listView1.SelectedItems[0].SubItems[2].Text;
    
                using (Form2 f2 = new Form2(this, p))
                {
                    if (f2.ShowDialog() == DialogResult.OK)
                    {
                        //all done!!
                    }
                }
            }
    
            public void UpdateListView(Person newPerson)
            {
                listView1.SelectedItems[0].Text = newPerson.First;
                listView1.SelectedItems[0].SubItems[1].Text = newPerson.Last;
                listView1.SelectedItems[0].SubItems[2].Text = newPerson.Age;
            }
        }
    
    //FORM 2:
        public partial class Form2 : Form
        {
            Person person;
            Form1 f1;
            public Form2()
            {
                InitializeComponent();
            }
    
            public Form2(Form1 _f1, Person _person)
                : this()
            {
                this.f1 = _f1;
                this.person = _person;
                ShowData();
            }
    
            private void ShowData()
            {
                textBox1.Text = person.First;
                textBox2.Text = person.Last;
                textBox3.Text = person.Age;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                person = new Person();
                if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "")
                {
                    person.First = textBox1.Text;
                    person.Last = textBox2.Text;
                    person.Age = textBox3.Text;
                    //do update:
                    f1.UpdateListView(person);
                    this.DialogResult = DialogResult.OK;
                }
                else
                    MessageBox.Show("Please all fields required insertion of data!");
            }
        }
    
    //and My class:
        public class Person
        {
            public string First { get; set; }
            public string Last { get; set; }
            public string Age { get; set; }
        }

    Hope it helps,

    bye


    Mitja

    • Proposed as answer by Bob Wu-MT Monday, March 12, 2012 9:00 AM
    • Marked as answer by Bob Wu-MT Tuesday, April 3, 2012 3:28 AM
    Wednesday, March 7, 2012 2:36 PM

All replies

  • You can do this by using ListView.SelectedItem and ListviewItem.tag properties. When you insert an item to the listview set item's tag property to the object you created in your form, then when user select an item from the listview you can obtain the object you created earlier by accessing listview.selecteditem.tag and casting it to the correct type.

    Bilhan silva

    Wednesday, March 7, 2012 1:39 PM
  • You can use the Tag property of the Listivew to store the customer object.

    On item selection for editing, get the object, cast it to customer and pass to the CustomerFrame

    After updation, call the MainForm.Refresh() method of listivew - to view the changed properties.

    Better to have a CustomerFrame method like this (with insert, update functionalities)

    bool Execute(bool isInsert, Customer customer);



    Resolving n Evolving in C# (http://jeanpaulva.com)

    Wednesday, March 7, 2012 1:43 PM
  • Hi, my code is an example code that has 3 columns in listView (firstname, lastname and age). It shows how to update selected rows.

    The new form (form2) opens on DOUBLE CLICK of the row, it show the data that  are currently in the listview, which you can alter and when you press button all new data are inserted (updated) in listview - of previously selected item!

    Do it this way:

    //FORM 1:
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                listView1.Columns.Add("First name", 100, HorizontalAlignment.Left);
                listView1.Columns.Add("Last name", 100, HorizontalAlignment.Left);
                listView1.Columns.Add("Age", -2, HorizontalAlignment.Left);
                listView1.View = View.Details;
                listView1.FullRowSelect = true;
    
                //adding some example data:
                listView1.Items.Add(new ListViewItem(new string[] { "first 1", "last 1", "20" }));
                listView1.Items.Add(new ListViewItem(new string[] { "first 2", "last 2", "22" }));
                listView1.Items.Add(new ListViewItem(new string[] { "first 3", "last 3", "18" }));
                listView1.Items.Add(new ListViewItem(new string[] { "first 4", "last 4", "25" }));
            }
    
            private void listView1_DoubleClick(object sender, EventArgs e)
            {
                Person p = new Person();
                p.First = listView1.SelectedItems[0].Text;
                p.Last = listView1.SelectedItems[0].SubItems[1].Text;
                p.Age = listView1.SelectedItems[0].SubItems[2].Text;
    
                using (Form2 f2 = new Form2(this, p))
                {
                    if (f2.ShowDialog() == DialogResult.OK)
                    {
                        //all done!!
                    }
                }
            }
    
            public void UpdateListView(Person newPerson)
            {
                listView1.SelectedItems[0].Text = newPerson.First;
                listView1.SelectedItems[0].SubItems[1].Text = newPerson.Last;
                listView1.SelectedItems[0].SubItems[2].Text = newPerson.Age;
            }
        }
    
    //FORM 2:
        public partial class Form2 : Form
        {
            Person person;
            Form1 f1;
            public Form2()
            {
                InitializeComponent();
            }
    
            public Form2(Form1 _f1, Person _person)
                : this()
            {
                this.f1 = _f1;
                this.person = _person;
                ShowData();
            }
    
            private void ShowData()
            {
                textBox1.Text = person.First;
                textBox2.Text = person.Last;
                textBox3.Text = person.Age;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                person = new Person();
                if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "")
                {
                    person.First = textBox1.Text;
                    person.Last = textBox2.Text;
                    person.Age = textBox3.Text;
                    //do update:
                    f1.UpdateListView(person);
                    this.DialogResult = DialogResult.OK;
                }
                else
                    MessageBox.Show("Please all fields required insertion of data!");
            }
        }
    
    //and My class:
        public class Person
        {
            public string First { get; set; }
            public string Last { get; set; }
            public string Age { get; set; }
        }

    Hope it helps,

    bye


    Mitja

    • Proposed as answer by Bob Wu-MT Monday, March 12, 2012 9:00 AM
    • Marked as answer by Bob Wu-MT Tuesday, April 3, 2012 3:28 AM
    Wednesday, March 7, 2012 2:36 PM
  • youre example is really good and i try to match it with my code but its rather hard look 

            private void listView1_DoubleClick(object sender, EventArgs e)
            {
                CustomerFrame customerframe = new CustomerFrame();
                CustomerFiles.Contact contact = customerframe.GetContact();
                CustomerFiles.Address address = customerframe.GetAddress();
                CustomerFiles.Phone phone = customerframe.GetPhone();
                CustomerFiles.Email email = customerframe.GetEmail();
    
                contact.FirstName = listView1.SelectedItems[0].Text;
                contact.LastName = listView1.SelectedItems[0].SubItems[1].Text;
                phone.Home = listView1.SelectedItems[0].SubItems[2].Text;
                phone.Mobile = listView1.SelectedItems[0].SubItems[3].Text;
                address.Country = listView1.SelectedItems[0].SubItems[4].Text;
                address.ZipCode= listView1.SelectedItems[0].SubItems[5].Text;
                address.City = listView1.SelectedItems[0].SubItems[6].Text;
                address.Street = listView1.SelectedItems[0].SubItems[7].Text;
                email.Personal = listView1.SelectedItems[0].SubItems[8].Text;
    
                using (CustomerFrame customframe = new CustomerFrame());
    
                if (customerframe.ShowDialog() == DialogResult.OK)
                {
                    //I want information of the item to be shown in its each textbox
                }
            }


    In your double click method i try to do the same but i can not.

    look further inside CustomerFrame

    internal CustomerFiles.Contact GetContact()
            {
                CustomerFiles.Contact contact = new CustomerFiles.Contact();
                contact.FirstName = tbFirstName.Text;
                contact.LastName = tbLastName.Text;
                
                return contact;
    
            }
            internal CustomerFiles.Address GetAddress()
            {
                address.City = tbCity.Text;
                address.Street = tbStreet.Text;
                address.ZipCode = tbZipCode.Text;
                address.country = cbCountry.Text;
    
                return address;
            }
            internal CustomerFiles.Phone GetPhone()
            {
                CustomerFiles.Phone phone = new CustomerFiles.Phone();
                phone.Home = tbHomePhone.Text;
                phone.Mobile = tbCellPhone.Text;
    
                return phone;
                
            }
            internal CustomerFiles.Email GetEmail()
            {
                CustomerFiles.Email email = new CustomerFiles.Email();
                email.Personal = tbEmail.Text;
    
                return email;
            }

    Here is a link of my classes 


    http://imageshack.us/photo/my-images/692/shit33.png/

    inside contact, phone and email class i have the properties 





    • Edited by donderma Thursday, March 8, 2012 4:23 PM
    Thursday, March 8, 2012 3:54 PM
  • Hi donderma,
    What do you mean by “In your double click method i try to do the same but i can not.”;
    I think there same error in your code.
    I’m not sure what the structure of the CustomerFrame class but if you pass the data to customerframe directly, you should pass it to the inner parameters, such as tbFirstName.
    If you want to pass the entire data to the customerframe instance, you should create a Constructor with parameters and pass it when you create an instance of CustomerFrame. For example,
      using (Form2 f2 = new Form2(this, p)) // rather than   using (Form2 f2 = new Form2())
    If there is anything unclear, please let us know.
    Best Regards,

    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

    Monday, March 12, 2012 9:00 AM