First, a class that represents an individual person should be declared.
The code below declares a class with the FirstName, LastName and Age public properties.
These properties will be data source fields.
class Person
{
private string _FirstName;
public string FirstName
{
get { return _FirstName; }
set { _FirstName = value; }
}
private string _LastName;
public string LastName
{
get { return _LastName; }
set { _LastName = value; }
}
private int _Age;
public int Age
{
get { return _Age; }
set { _Age = value; }
}
public Person(string firstName, string lastName, int age)
{
this.FirstName = firstName;
this.LastName = lastName;
this.Age = age;
}
}
Once the record class is declared, the data source object can be filled with records.
This example will use an ArrayList as the grid's data source. The code below fills a
List<Person> with records and assigns it to the DataGridView's DataGridView1.DataSource property.
List<Person> personList = new List<Person>();
personList.Add(new Person("Bill", "Gates", 55));
personList.Add(new Person("Joe", "Miller", 30));
personList.Add(new Person("Nataly", "Mix", 22));
this.dataGridView1.DataSource = personList;
After the above code has been executed, the DataGridView will look similar to the one displayed below.
No comments:
Post a Comment