static void Main()
{
// 通过调用 A 方法获取一个二维的数组,并显示它的所有元素
int[,] intArray = A();
for (int i = 0; i < intArray.GetLength(0); i++)
{
for (int j = 0; j < intArray.GetLength(1); j++)
Console.Write(intArray[i, j].ToString().PadLeft(3, ' '));
Console.WriteLine();
}
Console.ReadLine();
}
/// <summary>
/// 返回一个二维数组
/// </summary>
/// <returns></returns>
public static int[,] A()
{
// 虚拟一个 DataTable
DataTable fDataTable = new DataTable();
fDataTable.Columns.Add("Col1", typeof(Int32));
fDataTable.Columns.Add("Col2", typeof(Int32));
fDataTable.Columns.Add("Col3", typeof(Int32));
fDataTable.Rows.Add(new object[] { 1, 1, 5});
fDataTable.Rows.Add(new object[] { 1, 2, 4 });
fDataTable.Rows.Add(new object[] { 1, 3, 3 });
fDataTable.Rows.Add(new object[] { 1, 4, 2 });
fDataTable.Rows.Add(new object[] { 1, 5, 1 });
int rowCount = fDataTable.Rows.Count; // 行数
int colCount = fDataTable.Columns.Count; // 列数
int[,] intArry = new int[rowCount, colCount];
// 将 DataTable 的数据填充到数组中
for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
{
for (int colIndex = 0; colIndex < colCount; colIndex++)
intArry[rowIndex, colIndex] = (int)fDataTable.Rows[rowIndex][colIndex];
}
return intArry;
}
知识改变命运,奋斗成就人生!