DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("type2", typeof(string)));
dt.Columns.Add(new DataColumn("times", typeof(int)));
DataRow dr = dt.NewRow();
dr["type2"] = type2;
dr["times"] = times;
dt.Rows.Add(dr);为什么我运行时出现错误???错误:列“type2”不属于表 。

解决方案 »

  1.   

     DataTable table = new DataTable();
                DataColumn column1 = new DataColumn("Id", typeof(string));
                DataColumn column2 = new DataColumn("Name", typeof(String));
                DataColumn column3 = new DataColumn("TreeLevel", typeof(int));
                table.Columns.Add(column1);
                table.Columns.Add(column2);
                table.Columns.Add(column3);            DataRow fistRow = table.NewRow();
                fistRow["Id"] = Guid.Empty.ToString();
                fistRow["Name"] = "全部";
                fistRow["TreeLevel"] = 0;
                table.Rows.Add(fistRow);
      

  2.   

      没有type2这个字段呗
      

  3.   

    DataTable dt = new DataTable();
    dt.Columns.Add(new DataColumn("type2", typeof(string)));
    dt.Columns.Add(new DataColumn("times", typeof(int)));
    DataRow dr = dt.NewRow();
    dr["type2"] = “type2”;
    dr["times"] = 6;
    dt.Rows.Add(dr);
    直接就建立成功了啊
      

  4.   

    前面的高手不是已经说的很明白了吗?dr["type2"] = type2;
    dr["times"] = times;其中的对象 type2 和 times 是怎么来的?问题就在这里啊dt.Columns.Add(new DataColumn("type2", typeof(string)));
    这一句的意思貌似只是添加了个叫type2类型为string的列
    dr["type2"] = type2;
    这句你是要给新new的一行中的type2列赋值 但赋的值type2是什么东西? 如果要赋值为type2是不是加个引号,如果type2为变量,那么是不是需要先new出来赋值?
      

  5.   

    DataTable dt = new DataTable();
    dt.Columns.Add(new DataColumn("type2", typeof(string)));
    dt.Columns.Add(new DataColumn("times", typeof(int)));
    DataRow dr = dt.NewRow();
    dr["type2"] = type2;
    dr["times"] = times;
    dt.Rows.Add(dr)楼主这2个是啥意思??  变量??
    楼主 你代码是没问题的 只是你要搞清楚type2;times;是什么东西  一个string类型 和 int类型   只要类型正确  是不会报楼主说的 错误:列“type2”不属于表 。    
      

  6.   

    是啊,关键是搞清楚type2;times;是什么东西 
      

  7.   

    //old
    DataTable dt = new DataTable();
    dt.Columns.Add(new DataColumn("type2", typeof(string))); //创建string类型的列type2
    dt.Columns.Add(new DataColumn("times", typeof(int)));    //创建int类型的列times
    DataRow dr = dt.NewRow();
    dr["type2"] = type2;   //type2未定义,上面给出的type2只是列名,并非变量值 
    dr["times"] = times;   //times未定义
    dt.Rows.Add(dr);//new
    DataTable dt = new DataTable();
    dt.Columns.Add(new DataColumn("type2", typeof(string)));
    dt.Columns.Add(new DataColumn("times", typeof(int)));
    string type2 = "type2";
    int times = 0;
    DataRow dr = dt.NewRow();
    dr["type2"] = type2;
    dr["times"] = times;
    dt.Rows.Add(dr);
      

  8.   

    DataTable dt = new DataTable();
    dt.Columns.Add(new DataColumn("type2", typeof(string))); //创建string类型的列type2
    dt.Columns.Add(new DataColumn("times", typeof(int))); //创建int类型的列times
    DataRow dr = dt.NewRow();
    dr["type2"] = type2; //type2未定义,上面给出的type2只是列名,并非变量值  
    dr["times"] = times; //times未定义
    dt.Rows.Add(dr);11楼说的对。
    要分清楚列名和变量名,尽量不要用一样的名字,以免混淆。
      

  9.   

    dr["type2"] = “type2”;