ASP.NET-FineUI开发实践-13(一)
开原版好像是没有gird树,有一个扩展列下的模拟树列,就是不能展开,专业版有,开原版我弄了弄,思路是有,就是不是很好实现。这篇博客直接写出了(一)说明一个肯定是写不完的。
/// <summary> /// 是否启用树 /// </summary> [Category(CategoryName.OPTIONS)] [DefaultValue(null)] [Description("是否启用树")] public bool EnableTree { get { object obj = FState["EnableTree"]; return obj == null ? false : (bool)obj; } set { FState["EnableTree"] = value; if (value) { EnableRowClickEvent = true; } } } /// <summary> /// 树节点列名 /// </summary> [Category(CategoryName.OPTIONS)] [DefaultValue(null)] [Description("树节点列名")] public string TreeName { get { object obj = FState["TreeName"]; return obj == null ? "" : (string)obj; } set { FState["TreeName"] = value; } } /// <summary> /// 主键ID /// </summary> [Category(CategoryName.OPTIONS)] [DefaultValue(null)] [Description("主键ID")] public string TreeDataIDField { get { object obj = FState["TreeDataIDField"]; return obj == null ? "" : (string)obj; } set { FState["TreeDataIDField"] = value; } } /// <summary> /// 父节点列名 /// </summary> [Category(CategoryName.OPTIONS)] [DefaultValue(null)] [Description("父节点列名")] public string TreeDataParentIDField { get { object obj = FState["TreeDataParentIDField"]; return obj == null ? "" : (string)obj; } set { FState["TreeDataParentIDField"] = value; } }
/// <summary> /// 得到当然行的层级 /// </summary> /// <param name="row">当前行</param> /// <param name="Rows">行数据集</param> /// <returns>层级</returns> private int GetLevelNub(GridRow row, GridRowCollection Rows) { int lub = 0; //ID所在列 int IDindex = FindColumn(TreeDataIDField).ColumnIndex; //父ID所在列 int ParentIDindex = FindColumn(TreeDataParentIDField).ColumnIndex; //如果过是第第一级就是0 if (row.Values[ParentIDindex].ToString() == "" || row.Values[ParentIDindex].ToString() == "0") { return lub; } else { foreach (GridRow gr in Rows) { //如果有父级 if (gr.Values[IDindex].ToString() == row.Values[ParentIDindex].ToString()) { //层级加1 lub++; //查看父级的父级 int nub = GetLevelNub(gr, Rows); lub += nub == 0 ? 1 : nub; } } } return lub; }
<f:Grid EnableCollapse="true" Width="800px" Height="400px" runat="server" DataKeyNames="Id,Name" ID="Grid1" EnableTree="true" TreeName="name" TreeDataIDField="id" TreeDataParentIDField="topID" ShowBorder="true" ShowHeader="true" Title="树表格" > <Columns> <f:RowNumberField /> <f:BoundField ColumnID="name" ExpandUnusedSpace="true" DataField="name" HeaderText="名称" > </f:BoundField> <f:BoundField ColumnID="id" DataField="id" Width="50px" HeaderText="ID"> </f:BoundField> <f:BoundField ColumnID="topID" DataField="topID" Width="50px" HeaderText="topID"> </f:BoundField> <f:BoundField DataField="levelcode" Width="100px" HeaderText="层级编号"> </f:BoundField> <f:BoundField ColumnID="levelnub" DataField="levelnub" Width="100px" HeaderText="层级数"> </f:BoundField> <f:BoundField DataField="subitem" Width="100px" HeaderText="是否为子菜单"> </f:BoundField> <f:BoundField DataField="url" Width="100px" HeaderText="地址"> </f:BoundField> </Columns> </f:Grid>
/// <summary> /// 对rows进行重新排序 /// </summary> private void SortTreeRows() { GridRowCollection newrows = new GridRowCollection(); //复制到新列表 foreach (GridRow row in Rows) { newrows.Add(row); } //清空 Rows.Clear(); int pidindex = FindColumn(TreeDataParentIDField).ColumnIndex; int idindex = FindColumn(TreeDataIDField).ColumnIndex; //记录根节点 GridRowCollection onerows = new GridRowCollection(); //int i = 0; foreach (GridRow row in newrows) { if (row.Values[pidindex].ToString() == "" || row.Values[pidindex].ToString() == "0") { //row.RowIndex = i; //i++; //保存跟节点 Rows.Add(row); onerows.Add(row); } } int c = onerows.Count; //循环根节点插入子节点 for (int j = 0; j < c; j++) { GridRow row = onerows[j]; //注意起始index计算,从新插入的行开始下一次插入 BuildTree(Rows.Count - c + j, row, newrows); } for (int i = 0; i < Rows.Count; i++) { Rows[i].RowIndex = i; } } /// <summary> /// 重构行 /// </summary> /// <param name="j">插入的index</param> /// <param name="row">当前row</param> /// <param name="rows">集合</param> private void BuildTree(int j, GridRow row, GridRowCollection rows) { //得到下层节点index string nexindex = GetNextIndex(row, rows); if (nexindex != "") { string[] s = nexindex.Split(‘,‘); int c = Rows.Count; for (int i = 0; i < s.Length; i++) { GridRow dr = rows[Convert.ToInt32(s[i])]; //重新计算下一次插入的行号 int index = j + i + 1 + (Rows.Count - c); if (Rows.Count != c) { index = index - 1; } //dr.RowIndex = index; Rows.Insert(index, dr); //循环子节点 BuildTree(index, dr, rows); } } } /// <summary> /// 得到下级行号 /// </summary> /// <param name="row">本节点</param> /// <param name="Rows">集合</param> /// <returns>集合以,隔开</returns> private string GetNextIndex(GridRow row, GridRowCollection Rows) { string topindex = ""; int pridindex = FindColumn(TreeDataParentIDField).ColumnIndex; int idindex = FindColumn(TreeDataIDField).ColumnIndex; foreach (GridRow gr in Rows) { //父ID等于本ID添加到集合 if (gr.Values[pridindex].ToString() != "" && gr.Values[pridindex].ToString() == row.Values[idindex].ToString()) { topindex += topindex == "" ? gr.RowIndex.ToString() : "," + gr.RowIndex.ToString(); } } return topindex; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。