views:

37

answers:

3

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 ".)

A: 

It seems that you have missed adding the columns that you have created dynamically to your datatable. In the createdatatable function, you need a

inventorytable.Columns.Add(carmake)

after the line

`DataColumn carmake = new DataColumn("make", typeof(string));`
InSane
@In Sane yup i did the mistake of not adding the column ... now i have rectified the mistake....anyways thanx for pointing out the mistake!!!
smriti
A: 

You need to do the following:

DataColumn carmake = new DataColumn("make", typeof(string)); 
DataColumn carcolor = new DataColumn("color", typeof(string)); 
DataColumn carpetname = new DataColumn("petname", typeof(string));

// Your missing this part:
inventorytable.Columns.Add(carmake);
inventorytable.Columns.Add(carcolor);
inventorytable.Columns.Add(carpetname);

You forgot to add the columns to the table so when you create the row, it has no columns in it and your code fails the column name lookup.

Kelsey
@kelsey hey thanx for the correction,now that the code is working properly!!!
smriti
A: 

Hi,

You're creating the columns, but, not adding them to the table. So, you need to add the code mentioned by "Kelsey".

=========================================================
Always remember to mark an answer if you find it correct. This will encourage the community to answer your questions.

vamyip
@vamyip thanx i added the columns and the code is working ......
smriti