客户决定查看哪些字段,并且其中有一些字段是ForeignKey,所以在Table中显示数据时并不知道是否存在应该显示parent数据的列。
不想使用DataGird,于是写出了下面的代码,但是因为效率的原因,正考虑替代方案呢。
/// <summary>
///
/// </summary>
/// <param name="fk">二维数组</param>
/// <param name="val">欲查找的值</param>
/// <returns>-1未找到</returns>
private System.Int32 IndexOf(System.String[,] fk,
System.String val)
{
for(System.Int32 i = 0; i < fk.GetLength(0); i++)
if(val == fk[i, 2])
return i;
return -1;
}
/// <summary>
///
/// </summary>
/// <param name="fields"></param>
/// <param name="fkIndex">fk列在Table中的cell索引(cell显示顺序和待查询字段顺序相同)</param>
/// <returns>true:待查询字段中存在fk</returns>
private System.Boolean GetFK(System.String[] fields,
out System.Int32[] fkIndex,
out System.String[,] fkFields)
{
if(fields.Length == 0)//空查询字段
{
fkIndex = new System.Int32[0];
fkFields = new System.String[0,0];
return false;
}
System.String[,] fk = new System.String[2, 3];
fk[0, 0] = "iTTT"; //fk的父键某列名称
fk[0, 1] = "Rel_ttt"; //关系名称
fk[0, 2] = "iTTT"; //fk的列名称
fk[1, 0] = "txt";
fk[1, 1] = "REL_depID";
fk[1, 2] = "department";
......
System.Int32 numCurrent = 0;//待查询字段中存在的fk数量
System.Int32 num = fk.GetLength(0);
for(System.Int32 i2 = 0; i2 < num; i2++)
if(System.Array.IndexOf(fields, fk[i2, 2]) != -1)//fk
++numCurrent;
if(numCurrent == 0)//待查询字段中不包括fk
{
fkIndex = new System.Int32[0];
fkFields = new System.String[0, 0];
return false;
}
fkIndex = new System.Int32[numCurrent];
fkFields = new System.String[numCurrent, 2];
for(System.Int32 i3 = 0, iCurrent = -1; i3 < num; i3++)
{
System.Int32 index = System.Array.IndexOf(fields, fk[i3, 2]);//得到该fk在待查询字段中的索引
if(index == -1)//待查询字段中不包括该fk
continue;
fkIndex[++iCurrent] = index; //cell索引
fkFields[iCurrent, 0] = fk[this.IndexOf(fk, fk[i3, 2]), 0]; //在Table中显示该parent列
fkFields[iCurrent, 1] = fk[this.IndexOf(fk, fk[i3, 2]), 1]; //关系
}
return true;
}
/// <summary>
///
/// </summary>
/// <param name="rows"></param>
/// <param name="fields">待查询字段</param>
private void DisplayWebTable(System.Data.DataRow[] rows,
System.String[] fields)
{
this.DisplayWebTableHeader(fields);//打印表头
System.Boolean blnColor = true;
System.Int32[] ptIndex; //fk的cell索引
System.String[,] ptFields; //parent和relation
System.Boolean blnParent = this.GetFK(fields, out ptIndex, out ptFields);
for(System.Int32 fri = 0; fri < rows.Length; fri++)//行
{
...
for(System.Int32 fci = 0; fci < fields.Length; fci++)//列
{
System.Web.UI.WebControls.TableCell cell = new System.Web.UI.WebControls.TableCell();
if(!blnParent)//没有fk
{
cell.Controls.Add(
new System.Web.UI.LiteralControl(
rows[fri][fields[fci]].ToString()));
}
else
{
System.Int32 thisIndex = System.Array.IndexOf(ptIndex, fci);
if(thisIndex == -1)//cell不是fk cell
{
cell.Controls.Add(
new System.Web.UI.LiteralControl(
rows[fri][fields[fci]].ToString()));
}
else
{
System.Data.DataRow ptRow = rows[fri].GetParentRow(ptFields[thisIndex, 1]);
if(ptRow == null)
{
this.debug.Text = "发生致命错误";
return;
}
cell.Controls.Add(
new System.Web.UI.LiteralControl(
ptRow[ptFields[thisIndex, 0]].ToString()));
}
}
row.Cells.Add(cell);
}
this.resultTable.Rows.Add(row);
...
}
...
}