清单&LT;如何名单,LT字符串转换&GT;&GT;到DataTable [英] How to convert List<List<string>> to a DataTable
本文介绍了清单&LT;如何名单,LT字符串转换&GT;&GT;到DataTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将如何转换列表&LT;名单&LT;串&GT;&GT;
到数据表
?我想设置一个gridview到名单,LT的
变量数据源
;清单&LT;串&GT;&GT;
How would I convert List<List<string>>
to a DataTable
? I am trying to set the DataSource
of a gridview to a List<List<string>>
variable.
推荐答案
这可以很容易地使用扩展方法完成的。
This can easily be done using extension methods.
这个类添加到您的解决方案
Add this class to your solution
static class ListExtensions
{
public static DataTable ToDataTable(this List<List<string>> list)
{
DataTable tmp = new DataTable();
foreach (List<string> row in list)
{
tmp.Rows.Add(row.ToArray());
}
return tmp;
}
}
然后使用这样的扩展方法:
then use the extension method like this:
List<List<string>> myList = new List<List<string>>();
// Fill with values...
DataTable table = myList.ToDataTable();
这篇关于清单&LT;如何名单,LT字符串转换&GT;&GT;到DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文