c# 二维数组 datatable
时间: 2023-11-22 18:50:02 浏览: 80
以下是C#中二维数组转换为DataTable的方法:
方法一:
```csharp
// 假设二维数组为arr
DataTable dt = new DataTable();
for (int i = 0; i < arr.GetLength(1); i++)
{
dt.Columns.Add(new DataColumn("Column" + i.ToString()));
}
for (int i = 0; i < arr.GetLength(0); i++)
{
DataRow dr = dt.NewRow();
for (int j = 0; j < arr.GetLength(1); j++)
{
dr[j] = arr[i, j];
}
dt.Rows.Add(dr);
}
```
方法二:
```csharp
// 假设二维数组为arr
DataTable dt = new DataTable();
for (int i = 0; i < arr.GetLength(1); i++)
{
dt.Columns.Add(new DataColumn("Column" + i.ToString()));
}
for (int i = 0; i < arr.GetLength(0); i++)
{
dt.Rows.Add(arr[i, 0], arr[i, 1], arr[i, 2], ...);
}
```
关于C#中二维数组的使用方法,可以像下面这样:
```csharp
// 假设二维数组为arr
int[] arr1 = new int[arr.GetLength(0) * arr.GetLength(1)];
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
arr1[i * arr.GetLength(1) + j] = arr[i, j];
}
}
```
关于替换、查找的功能,可以使用C#中的数组方法和LINQ查询来实现。