Displaying ListView items - with class! by Rob Bi
2024-07-21 02:16:48
供稿:网友
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!