CHAPTER 22 ADO.NET PART II: THE DISCONNECTED LAYER
public class Car
{
public int ID { get; set; }
public string PetName { get; set; }
public string Make { get; set; }
public string Color { get; set; }
}
Within the default constructor of your main form, populate a List member variable (named
listCars) with a set of new Car objects, like so:
public partial class MainForm : Form
{
// A collection of Car objects.
List listCars = null;
public MainForm()
{
InitializeComponent();
}
}
// Fill the list with some cars.
listCars = new List
{
new Car { ID = 100, PetName = "Chucky", Make = "BMW", Color = "Green" },
new Car { ID = 101, PetName = "Tiny", Make = "Yugo", Color = "White" },
new Car { ID = 102, PetName = "Ami", Make = "Jeep", Color = "Tan" },
new Car { ID = 103, PetName = "Pain Inducer", Make = "Caravan",
Color = "Pink" },
new Car { ID = 104, PetName = "Fred", Make = "BMW", Color = "Green" },
new Car { ID = 105, PetName = "Sidd", Make = "BMW", Color = "Black" },
new Car { ID = 106, PetName = "Mel", Make = "Firebird", Color = "Red" },
new Car { ID = 107, PetName = "Sarah", Make = "Colt", Color = "Black" },
};
Next, add a new member variable named inventoryTable of type DataTable to your MainForm class
type, like so:
public partial class MainForm : Form
{
// A collection of Car objects.
List listCars = null;
// Inventory information.
DataTable inventoryTable = new DataTable();
...
}
Now add a new helper function to your class named CreateDataTable() and call this method within
the default constructor of the MainForm class.
879