Maz Hussain

Maz Hussain

  • NA
  • 5
  • 35.2k

Listview SelectedIndexChanged

Feb 11 2012 5:49 PM
Hi I am a newbie to C#, my current problem is how to use the selectedindexchanged property of a listview.

Basically I have a listview that is bounded on page load to the Customers table in the db.
What I am trying to achieve is when I select a specific customer on the listview, it should fetch data from the database for that specific customer and populate the textboxes so I can modify details.

I am programming using 3 tier procedures.

Any help would be appreciated. I basically want something like this int CustomerID = lstCustomers.selected????.value

So that I can pass the customer ID into another method and retrieve details.

my code is below:

    private void GetCustomerList()
        {
            List<Customer> customersList = new List<Customer>();

            try
            {
                customersList = CustomersDB.GetCustomers();
                if (customersList.Count > 0)
                {
                    Customer customer;
                    for (int i=0; i < customersList.Count; i++)
                    {
                        customer = customersList[i];
                        lstCustomers.Items.Add(customer.CustomerID.ToString());
                        lstCustomers.Items[i].SubItems.Add(customer.FirstName.ToString());
                        lstCustomers.Items[i].SubItems.Add(customer.LastName.ToString());
                        lstCustomers.Items[i].SubItems.Add(customer.DateJoined.ToShortDateString());
                        lstCustomers.Items[i].SubItems.Add(customer.Telephone.ToString());
                        lstCustomers.Items[i].SubItems.Add(customer.Email.ToString());
                    }
                }
                else
                {
                    MessageBox.Show("No customers exist in the database");
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.GetType().ToString());
                this.Close();
            }
        }

        private void lstCustomers_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

Thanks for any help


Answers (2)