Nevron Gauge for SharePoint
Skip Navigation Links
C# Corner Home
Forum Home
Latest 50
Unanswered
Win Prizes
All Time Leaders
Jump to CategoryExpand Jump to Category
Login 
    Welcome Guest!
 Search Forum For :  
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName
   Home » Arrays & Collections » Listview SelectedIndexChanged
       
Author Reply
Maz Hussain
posted 4 posts
since Aug 09, 2011 
from

Listview SelectedIndexChanged

  Posted on: 11 Feb 2012       
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

Vulpes
posted  5419 posts
since  Feb 28, 2011 
from 

 Re: Listview SelectedIndexChanged
  Posted on: 11 Feb 2012        2  
Assuming the ListView's MultiSelect property is false, I'd try:

private void lstCustomers_SelectedIndexChanged(object sender, EventArgs e)
{
   if (lstCustomers.SelectedItems.Count == 1)
   {
      int customerId = int.Parse(lstCustomers.SelectedItems[0].Text);
      // fetch data for that customer
   }
}    
Maz Hussain
posted  4 posts
since  Aug 09, 2011 
from 

 Re: Listview SelectedIndexChanged
  Posted on: 11 Feb 2012        0  
Wow, it works.

Thank you soooo much, there were so many different methods I was trying. 

.NET is so huge at the moment for me, I have much to learn but aspire to be a good developer one day.

Kind regards
       
Nevron Gauge for SharePoint
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Advertise with us
Current Version: 5.2011.3.12
 © 1999 - 2012  Mindcracker LLC. All Rights Reserved