最大的网站源码资源下载站,
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 framework 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.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;
}
}
// 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; }
}
}
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")));
}
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];
}
新闻热点
疑难解答