C#对象转dataTable 示例代码

private DataTable CreateTableBySignCitizenViewModel(List<SignCitizenViewModel> list)
{
  try
  {
    var dt = new DataTable();
    var array = new List<string>() { "QYZT", "JYTD" };
    array.ForEach(_ => dt.Columns.Add(_));
    foreach (var m in list)
    {
      var row = dt.NewRow();
      dt.Rows.Add(row);
      row["QYZT"] = m.QYZT;
      row["JYTD"] = m.JYTD;

 

    }
    return dt;
  }
  catch (Exception ex) { MessageBox.Show(ex.Message); }
  return null;
}

posted @ 2019-07-22 14:27  其善-从之  阅读(1687)  评论(0编辑  收藏  举报