问题描述
- link关于课程表的问题,如何将产生的课程表转换成DataTable?
- link关于课程表的问题,如何将产生的课程表转换成DataTable?
解决方案
DataTable dt = new DataTable();
dt.Columns.AddRange(result[0]);
foreach (var item in result.Skip(1))
{
var row = dt.NewRow();
for (int i = 0; i < 5; i++)
row[i] = item[i];
dt.Rows.Add(row);
}
解决方案二:
实现 List转换成DataSet : http://blog.csdn.net/q107770540/article/details/6556210
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; namespace WebApplication2 { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender EventArgs e) { if (!IsPostBack) { var list = new List<Demo> { new Demo{ id=1age=18 name=""Tim""} new Demo{ id=2age=22 name=""Allen""} new Demo{ id=3age=24 name=""Jim""} }; var ds = list.ToDataSet(); GridView1.DataSource = ds.Tables[0]; GridView1.DataBind(); } } } static class Extensions { internal static DataSet ToDataSet<T>(this IList<T> list) { Type elementType = typeof(T); var ds = new DataSet(); var t = new DataTable(); ds.Tables.Add(t); elementType.GetProperties().ToList().ForEach(propInfo => t.Columns.Add(propInfo.Name Nullable.GetUnderlyingType(propInfo.PropertyType) ?? propInfo.PropertyType)); foreach (T item in list) { var row = t.NewRow(); elementType.GetProperties().ToList().ForEach(propInfo => row[propInfo.Name] = propInfo.GetValue(item null) ?? DBNull.Value); t.Rows.Add(row); } return ds; } } class Demo { public int id { get; set; } public string name { get; set; } public int age { get; set; } } }
解决方案三:
接我之前的代码:
DataTable dt = new DataTable();
dt.Columns.AddRange(result[0]);
foreach (var item in result.Skip(1))
{
var row = dt.NewRow();
for (int i = 0; i < 5; i++)
row[i] = item[i];
}
时间: 2024-10-31 03:46:35