private DataTable SwapTable(DataTable tableData)
{
int intRows = tableData.Rows.Count;
int intColumns = tableData.Columns.Count;
//转二维数组
string[,] arrayData = new string[intRows, intColumns];
for (int i = 0; i < intRows; i++)
{
for (int j = 0; j < intColumns; j++)
{
arrayData[i, j] = tableData.Rows[i][j].ToString();
}
}
//下标对换
string[,] arrSwap = new string[intColumns, intRows];
for (int m = 0; m < intColumns; m++)
{
for (int n = 0; n < intRows; n++)
{
arrSwap[m, n] = arrayData[n, m];
}
}
DataTable dt = new DataTable();
//添加列
for (int k = 0; k < intRows; k++)
{
dt.Columns.Add(
new DataColumn(arrSwap[0, k])
);
}
//添加行
for (int r = 1; r < intColumns; r++)
{
DataRow dr = dt.NewRow();
for (int c = 0; c < intRows; c++)
{
dr[c] = arrSwap[r, c].ToString();
}
dt.Rows.Add(dr);
}
//添加行头
DataColumn ColRowHead = new DataColumn(tableData.Columns[0].ColumnName);
dt.Columns.Add(ColRowHead);
dt.Columns[ColRowHead.ColumnName].SetOrdinal(0);
for (int i = 0; i < intColumns - 1; i++)
{
//替换行头
List<MapperInfo> mapper = new List<MapperInfo>();
mapper = Resource.Mapper(OutSideForm.WEB_INPROT_DRUG);
string ColumnName = string.Empty;
foreach (var m in mapper)
{
if (m.ColumnName.Equals(tableData.Columns[i + 1].ColumnName))
{
ColumnName = m.Note;
break;
}
}
//dt.Rows[i][ColRowHead.ColumnName] = tableData.Columns[i + 1].ColumnName;
dt.Rows[i][ColRowHead.ColumnName] = ColumnName;
}
return dt;
}
DataTable行转列
最新推荐文章于 2026-06-19 11:28:59 发布

644

被折叠的 条评论
为什么被折叠?



