【问题标题】:How to convert list Object to DataTable object [duplicate]如何将列表对象转换为数据表对象[重复]
【发布时间】:2017-10-07 18:01:10
【问题描述】:

无法隐式转换类型 system.collections.generic.List 到 System.Data.DataTable

List<string> selectedCustomer = new List<string>();
selectedCustomer.Add(CustomerID);
selectedCustomer.Add(FirstName);
selectedCustomer.Add(LastName);

DataTable dt = selectedCustomer;

如何解决上述错误信息?

【问题讨论】:

标签: c# list datatable


【解决方案1】:
static DataTable GetTable()
{
    // Here we create a DataTable with four columns.
    DataTable table = new DataTable();
    table.Columns.Add("Dosage", typeof(int));
    table.Columns.Add("Drug", typeof(string));
    table.Columns.Add("Patient", typeof(string));
    table.Columns.Add("Date", typeof(DateTime));

    // Here we add five DataRows.
    table.Rows.Add(25, "Indocin", "David", DateTime.Now);
    table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
    table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
    table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
    table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
    return table;
}

HEPSİ ALINTIDIR!

【讨论】:

  • 请用英语回答问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多