[c#]代码库
#region 树状数据表,处理排序
//创建finaldata表和originaldata表,做为源表和处理后的表格
DataTable finaldata;
DataTable originaldata;
#region 获取表格
/// <summary>
/// 说明:查询数据库,获取表格
/// 作者:yxx
/// 时间:2013-11-12
/// </summary>
protected DataTable GetDataTable()
{
DataSet ds = Common.NoPager2("factorManagement", "", "ID,sort_parentID,sort_ID,sort_name,TreeLevel,Valided", "", "sortType=2 AND factorManagement.Valided=1");
DataTable dt = ds.Tables[0];
return dt;
}
#endregion
//调用处理方法
protected void HandledTable()
{
getdata();
//1为产品根
step(2);
//删除表中第一行
finaldata.Rows.RemoveAt(0);
}
//调用GetDataTable得到数据,并处理finaldata,originaldata表
protected void getdata()
{
finaldata = GetDataTable().Clone();
finaldata.Columns.Add("select", typeof(int));
originaldata = GetDataTable();
originaldata.Columns.Add("select", typeof(int));
}
//遍历过程
protected void step(int i)
{
//调用GetChildNode 获取子节点信息
DataTable child = GetChildNode(i);
int childCount = child.Rows.Count;
//找到本节点信息
DataRow[] sortAdd = originaldata.Select("Valided=1 and ID='" + i + "'");
DataRow sort = sortAdd[0];
if (childCount == 0)
{
sort["select"] = 1;
}
else
{
sort["select"] = 0;
}
// finaldata中添加本节点
finaldata.ImportRow((DataRow)sort);
//如存在子节点
if (childCount > 0)
{
for (int j = 0; j < child.Rows.Count; j++)
{
int k = Convert.ToInt16(child.Rows[j]["ID"].ToString());
step(k); //进行递归
}
}
}
//查找子节点
protected DataTable GetChildNode(int x)
{
DataRow[] sortChild = originaldata.Select("Valided=1 and sort_parentID='" + x + "'");
DataTable dt = originaldata.Clone();
for (int i = 0; i < sortChild.Length; i++)
{
dt.ImportRow((DataRow)sortChild[i]);
}
return dt;
}
#endregion
by: 发表于:2018-01-03 10:42:51 顶(0) | 踩(0) 回复
??
回复评论