Displaying ListView items - with class! by Rob Bir

80酷酷网    80kuku.com

  Displaying ListView items - with class!  by Rob Birdwell
I don't know about you, but I'm very fond of the ListView control - it's a very useful control to have around. Microsoft's .NET Common Language Runtime (CLR) provides much of the functionality you need to display data in a ListView. However, you're still left will some work to do. Loading the ListView and getting the data can be an arduous task. In fact, your code can get pretty messy unless you think things through a bit. In order to display complex class data, you should consider a way to associate your class data with each ListItem in the ListView.

Let's say you have a class as follows:

class Person                                                                               
   {                                                                                       
   public string Name;                                                                     
   public string Rank;                                                                     
   public string SerialNumber;                                                             
                                                                                           
   public Person (string n, string r, string sn)                                           
      {                                                                                    
      Name = n;                                                                            
      Rank = r;                                                                            
      SerialNumber = sn;  
      }                                                                            
   }

Your form requires you to display the Name, Rank, and SerialNumber members of each Person object in a ListView. We will now create a sub-class of the ListItem base class: "PersonListItem" class. This doesn't involve much code, and the pay-off will be cleaner code and greater flexibility if your display and/or design changes:

// Provide easy access to the Person class within a ListView                              
class PersonListItem : ListItem                                                           
   {                                                                                      
   private Person m_Person;                                                               
                                                                                          
   public PersonListItem (Person person) : base()                                         
      {                                                                                   
      m_Person = Person;                                                                  
                                                                                          
      // Assign Name to the base ListItem Text property -                                 
      // this will cause Name to display by default:                                      
      Text = m_Person.Name;                                                               
                                                                                          
      // Map the other class data to sub-items of the ListItem                            
      // These aren't necessarily displayed...                                            
      SetSubItem(0, m_Person.Rank);                                                        
      SetSubItem(1, m_Person.SerialNumber);                                                
      }                                                                                   
                                                                                          
   // Property for m_Person access                                                        
   public Person PersonData                                                               
      {                                                                                   
      get { return m_Person; }                                                            
      set { m_Person = value; }                                                           
      }                                                                                   
   }                                                                                      


Now all that remains is to add a ListView control to your form called PersonListView. We can optionally add three columns to the ListView: Name, Rank and Serial Number. If you add the columns, be sure to set the View property of PersonListView to "Report." In the form code, we'll add the PersonListItem objects via a simple helper function, called from the form's constructor, as follows:

private void InitPersonListView()                                                         
   {                                                                                      
                                                                                          
   // Load our PersonListView with our important people:                                  
                                                                                          
   PersonListView.InsertItem(PersonListView.ListItems.Count,                              
  new PersonListItem(new Person("Rob Birdwell", "Private", "123456")));                   
                                                                                          
   PersonListView.InsertItem(PersonListView.ListItems.Count,                              
  new PersonListItem(new Person("Bill Gates", "Chairman & CSA", "987654")));              
                                                                                          
   PersonListView.InsertItem(PersonListView.ListItems.Count,                              
   new PersonListItem(new Person("George Bush", "Commander-In Chief", "9999")));          
   }                                                                                      



The benefits of having a PersonListItem sub-class should be obvious - when a user clicks on an item, we have access to all the class data - not just the ListItem.Text property or sub-item data! Here's a sample event handler for our PersonListView's SelectedIndexChanged event:

protected void PersonListView_SelectedIndexChanged (object sender, System.EventArgs e)    
   {                                                                                      
   if (PersonListView.ListItems.Count == 0)                                               
      return; // nothing to do!                                                           
                                                                                          
   // Get the PersonListItem object associated with the selection                         
   PersonListItem li = (PersonListItem)GetCurrentSelectedListItem(PersonListView);        
   Person p = li.PersonData;                                                         
   MessageBox.Show( " Name: " + p.Name +                                                  
                    " Rank: " + p.Rank +                                                  
                    " Serial Number: " p.SerialNumber);                                   
   }                                                                                      
                                                                                          
// Here's a generic helper function for getting a ListView selection:                     
private ListItem GetCurrentSelectedListItem(ListView lv)                                  
   {                                                                                      
                                                                                          
   if (lv.SelectedItems == null || lv.SelectedItems.Count == 0)                           
      return null;                                                                        
                                                                                          
return lv.SelectedItems[0];                                                              
   }                                                                                      


If the PersonListView is defined with no columns, only the Person.Name member will be displayed since it was mapped to the PersonListItem.Text property in the constructor.

And that's what I call displaying ListView items - with class!


分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: