GridView控件

2008-08-05 14:06:53.0     推荐:0    收藏:0    评论:0     来源:中国IT实验室

支持树型的GridView

实现思路: 继承自Gridview,处理gridview的数据源,使其在帮定时,就已经按照树型菜单顺序排列好,那样只需在帮定处理其图片是javascript脚本就可以了

源代码下载:http://files.cnblogs.com/wschacker/TreeListView.rar

效果图:

Code


using System;
using System.Data;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.IO;
using System.Drawing;
using System.Drawing.Design;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
[ToolboxData("<{0}:TreeListView runat=server>"), DefaultProperty("Text")]
public class TreeListView : GridView, IPostBackDataHandler
{
public TreeListView()
{
base.AllowPaging = false;
base.AllowSorting = false;
base.AutoGenerateColumns = false;
}

Tree的属性设置#region Tree的属性设置
private int _nodeColumnIndex;
/**////


/// 显示树型的列 Index
///

public int NodeColumnIndex
{
get { return this._nodeColumnIndex; }
set
{
_nodeColumnIndex = value;
}
}

private string _columnKey;
/**////


/// Key字段
///

public string ColumnKey
{
get { return _columnKey; }
set
{
_columnKey = value;
}
}

private string _parentKey;
/**////


/// 指向父节点的字段
///

public string ParentKey
{
set
{
_parentKey = value;
}
}

private string _sortKey;
/**////


/// 排序字段
///

public string SortKey
{
set { _sortKey = value; }
}

private object _rootNodeFlag;
/**////


/// 根节点的标记 这里采用 ParentKey为什么字符
///

public object RootNodeFlag
{
set
{
_rootNodeFlag = value;
}
}

private static string _treeImageFolder = "/Images/Tree/";
public static string TreeImageFolder
{
get
{
return _treeImageFolder;
}
set
{
_treeImageFolder = value;
}
}

private int _expendDepth = 0;
/**////


/// 展开的深度
///

public int ExpendDepth
{
get
{
return _expendDepth;
}
set { _expendDepth = value; }
}
#endregion

public override object DataSource
{
get
{
return base.DataSource;
}
set
{
DataTable dtSource = new DataTable();
if (value is DataSet && ((DataSet)value).Tables.Count > 0)
{
DataSet ds = value as DataSet;
dtSource = OrderData(ds.Tables[0]);
}
else
{
throw new Exception("DataSource is not DataSet!");
}
base.DataSource = dtSource;
}
}

DataTable OrderData(DataTable dtSource)
{
DataTable dtResult = dtSource.Clone();
dtResult.Columns.Add("TreeListView$Row$Depth", typeof(int));
dtResult.Columns.Add("TreeListView$Row$IsLeaf", typeof(bool));
dtResult.Columns.Add("TreeListView$Row$IsBottom", typeof(bool));
dtResult.Columns.Add("TreeListView$Row$ParentRow", typeof(DataRow));
dtResult.Columns.Add("TreeList$ViewRow$ClientID", typeof(string));
RecursionOrderData(dtSource, dtResult, _rootNodeFlag, -1, null);
return dtResult;
}
string FormatToRowFilter(object val)
{
Type type = val.GetType();
if (type == typeof(string))
{
return string.Format("'{0}'", val.ToString().Replace("'", "''"));
}
else if (type == typeof(Guid))
{
return string.Format("'{0}'", val);
}
else if (type.IsValueType)
{
return val.ToString();
}
else
{
return string.Format("'{0}'", val.ToString().Replace("'", "''"));
}
}
bool RecursionOrderData(DataTable dtSource, DataTable dtResult, object parentID, int depth, DataRow parentDatarow)
{
DataView dv = new DataView(dtSource);
dv.RowFilter = string.Format("{0}={1}", _parentKey, FormatToRowFilter(parentID));
dv.Sort = _sortKey;
DataRow dr = null;
depth++;
for (int i = 0; i < dv.Count; i++)
{
dr = dtResult.NewRow();

for (int j = 0; j < dv[i].Row.ItemArray.Length; j++)
{
dr[j] = dv[i][j];
}

if (i == dv.Count - 1) //isBottom
{
dr["TreeListView$Row$IsBottom"] = true;
}
else
{
dr["TreeListView$Row$IsBottom"] = false;
}
dr["TreeListView$Row$Depth"] = depth;
dr["TreeListView$Row$ParentRow"] = parentDatarow;
if (depth == 0)
{
dr["TreeList$ViewRow$ClientID"] = Guid.NewGuid().ToString();
}
else
{
dr["TreeList$ViewRow$ClientID"] = parentDatarow["TreeList$ViewRow$ClientID"].ToString() + "/" + Guid.NewGuid().ToString();
}

dtResult.Rows.Add(dr);
dr["TreeListView$Row$IsLeaf"] = !RecursionOrderData(dtSource, dtResult, dv[i][_columnKey], depth, dr);
}

return dv.Count > 0;
}

public override bool AllowPaging
{
get
{
return base.AllowPaging;
}
set
{
base.AllowPaging = false;
}
}

public override bool AutoGenerateColumns
{
get
{
return base.AutoGenerateColumns;
}
set
{
base.AutoGenerateColumns = false;
}
}

重载:CreateRow#region 重载:CreateRow
protected override GridViewRow CreateRow(int rowIndex, int dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState)
{
return new TreeListViewRow(rowIndex, dataSourceIndex, rowType, rowState);
}
#endregion

[第1页]   [第2页]   [下一页]
您可以针对本文进行:[评论]  [收藏]  [推荐]  
  • 共有0条评论  点击查看更多评论
  • 网友评论仅供网友表达个人看法,并不表明e800同意其观点或证实其描述
我想发表评论:
用户名密码
  • 匿名发表
    验证码: