Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I may be wrong with this question, but is it possible to bind a custom class, lets say a Persons class to listview items?
For instance, lets assume we got this class:

VB
Public Class Person
  Public Property Name As String
  Public Property Age As Integer
  Public Property Sex As String
End Class


Is it possible to bind them to a listview control so it display each value in a column, and whatever i change a person's value it will automatically update on the listview control?
Posted
Comments
Sunasara Imdadhusen 9-Feb-11 23:54pm    
Good question!!

 
Share this answer
 
v3
Comments
creizlein 10-Feb-11 0:45am    
Private LVManualGen As Gizmox.WebGUI.Forms.ListView
I guess we are talking about custom ListView control in that context :(
Hi
Here is simple solution.

1) Have the person class
C#
public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string Sex { get; set; }

}


2) Have a static person collection class
C#
public static class PersonCollection
{
    private static IList<Person> persons = new List<Person>();
    public static void Add(Person person)
    {
        persons.Add(person);
    }
    public static IList<Person> GetAll()
    {
        return persons;
    }
}


3) Add a object data source to your aspx page.Click Configure datasource. The choose PersonCollection object as source and click next. In the select tab choose the method 'GetAll'. Finish the configurations.

4) Add a list view to your aspx page. Set the data source. Click configure list view (nothing to change) and then click ok.

5)In the page load event add sample data...
C#
Person person1 = new Person();
person1.Name = "Albin";
person1.Age = 10;
person1.Sex = "Male";
Person person2 = new Person();
person2.Name = "Augustina";
person2.Age = 5;
person2.Sex = "Female";

PersonCollection.Add(person1);
PersonCollection.Add(person2);

Thats all. These new person objects will list in the list view
 
Share this answer
 
v2
Comments
creizlein 10-Feb-11 0:43am    
I guess that the ASP Listview Control behaves totally different, i didn't test your code but i assume it works, but it does not in WinForms... i was asking my question to WinForm ListView
Albin Abel 10-Feb-11 1:11am    
Sorry,
In that case Imdadhusen's solution might works

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900