gpt4 book ai didi

c# - 如何将 List> 转换为 DataTable

转载 作者:太空狗 更新时间:2023-10-30 00:34:53 25 4
gpt4 key购买 nike

我将如何转换 List<List<string>>DataTable ?我正在尝试设置 DataSource gridview 到 List<List<string>>变量。

最佳答案

这可以使用扩展方法轻松完成。

将这个类添加到你的解决方案中

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;
}
}

然后像这样使用扩展方法:

List<List<string>> myList = new List<List<string>>();
// Fill with values...
DataTable table = myList.ToDataTable();

关于c# - 如何将 List<List<string>> 转换为 DataTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6344939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com