尝试填充数据表时收到错误列' ID'不属于表Employee。
namespace Demo.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
CreateTable ct = new CreateTable();
CreateColumn column = new CreateColumn();
column.CreateColumns();
ct.AddRow("1","Dan White","UK","857495260","RandD","78954000");
ct.AddRow("2","James","UK","7564210","Accounting","80000000");
return View();
}
}
}
namespace Demo.Models
{
public class CreateTable
{
private DataTable dataTable = new DataTable("Employee");
public void AddColumn(Type type,string name)
{
dataTable.Columns.Add(name,type);
}
public void AddRow(string id,string name,string address, string phone, string department,string salary)
{
var row = dataTable.NewRow();
row["ID"] = Int32.Parse(id);//Error in this line
row["Name"] = name;
row["Address"] = address;
row["Phone"] = Int64.Parse(phone);
row["Dept"] = department;
row["Salary"] = salary;
dataTable.Rows.Add(row);
}
}
public class CreateColumn
{
public void CreateColumns()
{
CreateTable ct = new CreateTable();
ct.AddColumn(typeof(Int32),"ID");
ct.AddColumn(typeof(String),"Name");
ct.AddColumn(typeof(String), "Address");
ct.AddColumn(typeof(Int64),"Phone");
ct.AddColumn(typeof(String),"Dept");
ct.AddColumn(typeof(Decimal),"Salary");
}
}
}
运行CreateColumns()后,(https://preview.ibb.co/h62OEn/Data_visvalizer_of_table.png)Data Visualizer 错误
期间的答案 0 :(得分:1)
修改CreateColumns
方法以获取CreateTable
类型的参数:
public class CreateColumn
{
public void CreateColumns(CreateTable ct)
{
ct.AddColumn(typeof(Int32),"ID");
ct.AddColumn(typeof(String),"Name");
ct.AddColumn(typeof(String), "Address");
ct.AddColumn(typeof(Int64),"Phone");
ct.AddColumn(typeof(String),"Dept");
ct.AddColumn(typeof(Decimal),"Salary");
}
}