Hey, I am trying to bind my data table inventorytable to the datagrid viewer.the problem in the code is when it compiles,it says the column"make" does not belong to the table but I make the column in the code as follows:
public partial class Form1 : Form
{
List<Car> listCars = new List<Car>();
DataTable inventorytable=new DataTable();
public Form1()
{
InitializeComponent();
listCars.Add(new Car("Chucky", "BMW", "Green"));
listCars.Add(new Car("Tiny", "Yugo", "White"));
listCars.Add(new Car("Ami", "Jeep", "Tan"));
listCars.Add(new Car("Pain Inducer", "Caravan", "Pink"));
listCars.Add(new Car("Fred", "BMW", "Pea Soup Green"));
listCars.Add(new Car("Sidd", "BMW", "Black"));
listCars.Add(new Car("Mel", "Firebird", "Red"));
listCars.Add(new Car("Sarah", "Colt", "Black"));
createdatatable();
}
void createdatatable()//create data table schema
{
DataColumn carmake = new DataColumn("make", typeof(string));
DataColumn carcolor = new DataColumn("color", typeof(string));
DataColumn carpetname = new DataColumn("petname", typeof(string));
foreach (Car c in listCars)
{
DataRow dr = inventorytable.NewRow();
**dr["make"] = c.carMake;**//column 'make' does not belong to the table.
dr["color"] = c.carColor;
dr["petname"] = c.carPetName;
inventorytable.Rows.Add(dr);
}
dataGridView1.DataSource = inventorytable;//binds data table to the grid view
}
}
}
I have also added a class to this form named as car whose code is as follows:
class Car
{
public string carPetName { get; set; }
public string carMake { get; set; }
public string carColor { get; set; }
public Car(string petname, string make, string color)
{
carPetName = petname;
carMake = make;
carColor = color;
}
please help!!!!!!!! P.S. (I wrote the statement as
DataTable inventorytable=new DataTable();
because when I wrote it as DataTable inventorytable
, it was giving an exception as
"object refence not set to an instance of an object ".)