Fill listview with references

  • Thread starter Thread starter kenny.deneef
  • Start date Start date
K

kenny.deneef

Hey,

I'm searching for a methode to get the the reference to an object I
added into a listview.

I got this class Address
class Address
{
private string street;
private string numb;
private string city;
private string country;
private string phone;
private string gsm;
private string email;
private string fax;

// and so on.....
// all got a property to acces
public Address(string street, string numb, string city, string
country, string phone, string gsm, string email, string fax)
{
this.street = street;
this.numb = numb;
this.city = city;
this.country = country;
this.phone = phone;
this.gsm = gsm;
this.email = email;
this.fax = fax;
}

public ArrayList ToItemList()
{
return new ListViewItem(new string[] { this.Naam, this.Voornaam,
this.Email }, -1);
}
}

In my mainform i got a listview where the street, numb and city are

ArrayList addressList = new ArrayList();
addressList.Add(new Address("street0", "numb0", "city0", "country0",
"phone0", "gsm0", "email0", "fax0"));
addressList.Add(new Address("street1", "numb1", "city1", "country1",
"phone1", "gsm1", "email1", "fax1"));
addressList.Add(new Address("street2", "numb2", "city2", "country2",
"phone2", "gsm2", "email2", "fax2"));
addressList.Add(new Address("street3", "numb3", "city3", "country3",
"phone3", "gsm3", "email3", "fax3"));

listView.Items.Clear();
foreach (Address address in addressList)
{
listView.Items.Add(address.ToItemList());
}

In the listview is now shown
street0 numb0 city0
street1 numb1 city1
street2 numb2 city2
street3 numb3 city3

The problem is now i can't get the other datamebers of a selected
item.
When an items is selected some textfields should get the value of
street, numb, city, country, phone, gsm, email and fax.

Thanks in advanced
 
when you convert the to arrayList's you lose all that other information.
I believe that you will have to perform a lookup to reconnect that list item
to the address item.
 
when you convert the to arrayList's you lose all that other information.
I believe that you will have to perform a lookup to reconnect that list item
to the address item.

And how do i perform such a lookup?
 
in your postback you would have to load your datasource

ArrayList addressList = new ArrayList();
addressList.Add(new Address("street0", "numb0", "city0", "country0",
"phone0", "gsm0", "email0", "fax0"));
addressList.Add(new Address("street1", "numb1", "city1", "country1",
"phone1", "gsm1", "email1", "fax1"));
addressList.Add(new Address("street2", "numb2", "city2", "country2",
"phone2", "gsm2", "email2", "fax2"));
addressList.Add(new Address("street3", "numb3", "city3", "country3",
"phone3", "gsm3", "email3", "fax3"));

and then find the one that correlates the the selected item

ListViewItem selectedItem = listView.selectedItem;

Address selectedAddress = null;
foreach(Address a in addressList)
{
if ((a.street == selectedItem[0] ) &&
(a.number == selectedItem[1]) &&
(a.city == selectedItem[2])
{
//then we found it
selectedAddress = a;
break;
}
}
}

now u know which address the selected list item correlates to, so u can get
the other data.

this looping method isnt overly efficient though..
 
Back
Top