C# 如何用列表填充数据表<;T>;
如何将列表转换为数据表C# 如何用列表填充数据表<;T>;,c#,ado.net,datatable,C#,Ado.net,Datatable,如何将列表转换为数据表 [Serializable] public class Item { public string Name { get; set; } public double Price { get; set; } public string @URL { get; set; } public Item(string Name, string Price, string @URL) { this.Name = Name;
[Serializable]
public class Item
{
public string Name { get; set; }
public double Price { get; set; }
public string @URL { get; set; }
public Item(string Name, string Price, string @URL)
{
this.Name = Name;
this.Price = Convert.ToDouble(Price);
this.@URL = @URL;
}
public override string ToString()
{
return this.Name;
}
}
我尝试使用:
static DataTable ConvertToDatatable(List<Item> list)
{
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Price");
dt.Columns.Add("URL");
foreach (var item in list)
{
dt.Rows.Add(item.Name, Convert.ToString(item.Price), item.URL);
}
return dt;
}
静态数据表ConvertToDatatable(列表)
{
DataTable dt=新的DataTable();
dt.列。添加(“名称”);
dt.列。添加(“价格”);
添加(“URL”);
foreach(列表中的变量项)
{
dt.Rows.Add(item.Name,Convert.ToString(item.Price),item.URL);
}
返回dt;
}
现在我看到一个盒子,但它是空的!救命!!我该怎么做才能使这个框实际包含数据?试试这个
static DataTable ConvertToDatatable(List<Item> list)
{
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Price");
dt.Columns.Add("URL");
foreach (var item in list)
{
var row = dt.NewRow();
row["Name"] = item.Name;
row["Price"] = Convert.ToString(item.Price);
row["URL"] = item.URL;
dt.Rows.Add(row);
}
return dt;
}
静态数据表ConvertToDatatable(列表)
{
DataTable dt=新的DataTable();
dt.列。添加(“名称”);
dt.列。添加(“价格”);
添加(“URL”);
foreach(列表中的变量项)
{
var row=dt.NewRow();
行[“名称”]=项目名称;
行[“价格”]=转换为字符串(项目价格);
行[“URL”]=item.URL;
dt.行。添加(行);
}
返回dt;
}
我还必须想出另一种解决方案,因为这里列出的选项在我的案例中都不起作用。我使用的是IEnumerable,基础数据是IEnumerable,无法枚举属性。这就成功了:
// remove "this" if not on C# 3.0 / .NET 3.5
public static DataTable ConvertToDataTable<T>(this IEnumerable<T> data)
{
List<IDataRecord> list = data.Cast<IDataRecord>().ToList();
PropertyDescriptorCollection props = null;
DataTable table = new DataTable();
if (list != null && list.Count > 0)
{
props = TypeDescriptor.GetProperties(list[0]);
for (int i = 0; i < props.Count; i++)
{
PropertyDescriptor prop = props[i];
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
}
}
if (props != null)
{
object[] values = new object[props.Count];
foreach (T item in data)
{
for (int i = 0; i < values.Length; i++)
{
values[i] = props[i].GetValue(item) ?? DBNull.Value;
}
table.Rows.Add(values);
}
}
return table;
}
//如果不是在C#3.0/.NET 3.5上,请删除“this”
公共静态数据表ConvertToDataTable(此IEnumerable数据)
{
列表=data.Cast().ToList();
PropertyDescriptorCollection props=null;
DataTable=新的DataTable();
if(list!=null&&list.Count>0)
{
props=TypeDescriptor.GetProperties(列表[0]);
for(int i=0;i
以防类对象中有可为空的属性:
private static DataTable ConvertToDatatable<T>(List<T> data)
{
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
for (int i = 0; i < props.Count; i++)
{
PropertyDescriptor prop = props[i];
if (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
table.Columns.Add(prop.Name, prop.PropertyType.GetGenericArguments()[0]);
else
table.Columns.Add(prop.Name, prop.PropertyType);
}
object[] values = new object[props.Count];
foreach (T item in data)
{
for (int i = 0; i < values.Length; i++)
{
values[i] = props[i].GetValue(item);
}
table.Rows.Add(values);
}
return table;
}
私有静态数据表ConvertToDatatable(列表数据)
{
PropertyDescriptorCollection props=TypeDescriptor.GetProperties(typeof(T));
DataTable=新的DataTable();
for(int i=0;i
您说itemDataView
为空。它到底是什么?它从哪里来?这对于将列表
转换为数据表
项目来说似乎是另一个问题。数据视图是由windowsform Toolbox生成的。请澄清您的问题。您的标题显示您在生成数据表时遇到问题。但这似乎根本不是问题所在。您的问题是itemDataView
。我还以为是DataTable我仍然得到DataGridView为空的消息,那么您的问题不是转换,而是添加到表单中的DataGridView
控件。尝试删除它,然后重新添加,并将其名称更改为有意义的名称。这样做很有效,谢谢!!抱歉,这是我在datagridview中删除并读取的代码worked@alexw谢谢,对我来说很好。