Click event on listView

Posted on 2005-04-26
Last Modified: 2009-07-29
Language: C#

I have a listView control on windows form. When user click a row, i want to fire the event and get data from this row. eg.

ID       Name
1        Peter
2       Ben

when user click first row, i will get "1" and "Peter" ect. ect..

How can I do this? Experts!

Question by:vbwang
    LVL 9

    Accepted Solution

    First you need to hook up to the 'Click' event on the ListView as so:

          ListView lv = new ListView();
          lv.Click += new System.EventHandler(lv_Click);

    This will make the below method be called whenever the Click event is fired.

    private void lv_Click(object sender, System.EventArgs e){
          //Get data:
          string output = lv.FocusedItem.Text; //Gives the 'ID'
          foreach (ListViewItem.ListViewSubItem item in lv.FocusedItem.SubItems)
                output += ", " + item.Text; //Any other data.

          //Fire Event.
          if(RowClicked != null)
                RowClicked(this, new RowClickEventArgs(output));

    In the above method another event 'RowClicked' is fired, this is defined below. It takes a special EventHandler class that takes a string as a construction argument. You would handle this in whatever app the same way as you handled the ListView.Click event. (i.e. += to it).

    //The event which will be fired from the Click handler.
    public event RowClickEventHandler RowClicked;

    public delegate void RowClickEventHandler(object sender, RowClickEventArgs args);
    public class RowClickEventArgs : EventArgs
          private string mText;
          public string Text { get { return mText; } }
          public RowClickEventArgs(string text)
                mText = text;

    So in my secondary app I would do this somewhere:

    yourClass.RowClicked += new RowClickEventHandler(myHandler);

    private void myHandler(object sender, RowClickEventArgs e){


    LVL 1

    Expert Comment

    better u can use the selected index changed event.
    also,don't forget to set the multiselect property of listview to false.

    listview.selectedinedexchanged += new System.eventhandler(listview_selectedindexchanged);

    private void listview_selectedindexchanged(.....)
         if(listview.selecteditems.count == 0) // don't forget this line as the event will be fired twice when you change a selection.
         string ID = listview.selecteditems[0].subitems[0].Text;
         string name = listview.selecteditems[0].subitems[1].Text;
         // do what you want with the strings...
    LVL 9

    Expert Comment

    But SelectedIndexChanged would also fire if you use the keyboard to move the selection, not just a mouse click.

    Author Comment

    Thanks Wint. This is excellent  answer!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    This video discusses moving either the default database or any database to a new volume.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now