今天熟悉了下二维数组的使用,大概使用过程:就是先把DataTable数据存入到二维数组,然后再把二维数组循环读取出来。
测试代码如下(测试代码没做异常处理):
///获取DataTable数据集
DataTable dt = GetData();
//定义二维数组,并把数据存储到数组(根据DataTable行列数)
string[,] arrayList = new string[dt.Rows.Count,dt.Columns.Count];
for(int i=0;i<dt.Rows.Count;i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
arrayList[i,j] = dt.Rows[i][j].ToString();
}
}
//读取二维数组数据并循环显示(说明:arrayList.GetLength(0):获取数组行数,arrayList.GetLength(1):获取列数行数)
string strRes = "";
for (int i = 0; i < arrayList.GetLength(0); i++)
{
for (int j = 0; j < arrayList.GetLength(1); j++)
{
strRes += arrayList[i, j] + " ";
}
strRes += "<br>";
}
//输出
Response.Write(strRes);
/// <summary>
/// 获取数据集
/// </summary>
/// <returns>DataTable数据集</returns>
DataTable GetData()
{
DataTable dt = new DataTable();
DataColumn dc;
DataRow dr;
dc=new DataColumn();
dc.ColumnName="ID";
dc.DataType = typeof(int);
dc.DefaultValue = null;
dt.Columns.Add(dc);
dc=new DataColumn();
dc.ColumnName="Name";
dc.DataType = typeof(string);
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "Address";
dc.DataType = typeof(string);
dt.Columns.Add(dc);
dt.Columns.Add(new DataColumn("School"));
dr=dt.NewRow();
dr["ID"]="1";
dr["Name"]="小明";
dr["Address"] = "广东";
dr["School"] = "广东大学";
dt.Rows.Add(dr);
dr=dt.NewRow();
dr["ID"]="2";
dr["Name"]="林明";
dr["Address"] = "深圳";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["ID"] = "3";
dr["Name"] = "张明";
dr["Address"] = "江西";
dt.Rows.Add(dr);
return dt;
}