【问题标题】:Problem creating datatable via code and binding to a datagridview通过代码创建数据表并绑定到 datagridview 的问题
【发布时间】:2010-07-27 01:48:21
【问题描述】:

我正在尝试将我的数据表库存表绑定到数据网格查看器。代码中的问题是当它编译时,它说“make”列不属于该表,但我在代码中按如下方式创建列:

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

        }

    }
}

我还在这个表单中添加了一个名为 car 的类,其代码如下:

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;
        }

请帮忙!!!!!!!!!附言(我把声明写成

DataTable inventorytable=new DataTable();

因为当我把它写成DataTable inventorytable 时,它给出了一个例外 “对象引用未设置为对象的实例”。)

【问题讨论】:

    标签: c# winforms datatable


    【解决方案1】:

    您需要执行以下操作:

    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);
    

    您忘记将列添加到表中,因此当您创建行时,其中没有列,并且您的代码无法查找列名。

    【讨论】:

      【解决方案2】:

      您似乎错过了将动态创建的列添加到数据表中。 在createdatatable 函数中,你需要一个

      inventorytable.Columns.Add(carmake)
      

      行后

      `DataColumn carmake = new DataColumn("make", typeof(string));`
      

      【讨论】:

      • @In Sane yup 我犯了不添加列的错误......现在我已经纠正了这个错误......无论如何感谢指出错误!!!
      【解决方案3】:

      您正在创建列,但并未将它们添加到表中。所以,你需要添加“Kelsey”提到的代码。

      ================================================ ==========
      如果您认为答案正确,请务必记住标记答案。 这将鼓励社区回答您的问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-10
        • 1970-01-01
        • 2014-05-21
        • 1970-01-01
        • 2021-06-16
        • 2022-06-11
        相关资源
        最近更新 更多