1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
public partial class MainForm : Form
{
// A collection of Car objects.
List<Car> listCars = new List<Car>();
// Inventory information
DataTable inventoryTable = new DataTable();
// View of the DataTable.
DataView yugosOnlyView;
public MainForm()
{
InitializeComponent();
// Fill the list with some cars.
listCars = new List<Car>
{
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" },
};
CreateDataTable();
}
#region Create the data table
void CreateDataTable()
{
// Create table schema.
DataColumn carIDColumn = new DataColumn("ID", typeof(int));
DataColumn carMakeColumn = new DataColumn("Make", typeof(string));
DataColumn carColorColumn = new DataColumn("Color", typeof(string));
DataColumn carPetNameColumn = new DataColumn("PetName", typeof(string));
carPetNameColumn.Caption = "Pet Name";
inventoryTable.Columns.AddRange(new DataColumn[] { carIDColumn, carMakeColumn,
carColorColumn, carPetNameColumn });
// Iterate over the array list to make rows.
foreach (Car c in listCars)
{
DataRow newRow = inventoryTable.NewRow();
newRow["ID"] = c.ID;
newRow["Make"] = c.Make;
newRow["Color"] = c.Color;
newRow["PetName"] = c.PetName;
inventoryTable.Rows.Add(newRow);
}
// Bind the DataTable to the carInventoryGridView.
carInventoryGridView.DataSource = inventoryTable;
} |