源码网,源码论坛,源码之家,商业源码,游戏源码下载,discuz插件,棋牌源码下载,精品源码论坛

 找回密码
 立即注册
楼主: ttx9n

[ASP.NET] TreeView创建IHierarchicalDataSource类型的数据源实现

[复制链接]

7万

主题

861

回帖

32万

积分

论坛元老

Rank: 8Rank: 8

积分
329525
发表于 2009-1-9 01:02:21 | 显示全部楼层 |阅读模式
为TreeView创建IHierarchicalDataSource类型的数据源实现 IHierarchicalDataSource实现:
public class InsHierarchyData : IHierarchyData, ICustomTypeDescriptor
{
private DataRowView item;
private string dataParentIdField;
private string dataIdField;
private string displayField;
public InsHierarchyData(string dataParentIdField, string dataIdField,string displayField, DataRowView item)
{
this.item = item;
this.dataParentIdField = dataParentIdField;
this.dataIdField = dataIdField;
this.displayField = displayField;
}
bool IHierarchyData.HasChildren
{
get
{
foreach (DataRowView row in item.DataView)
{
object[] array1 = row.Row.ItemArray;
object[] array2 = item.Row.ItemArray;
string a = row[dataParentIdField].ToString();
string b = item[dataIdField].ToString();
if (a == b)
return true;
}
return false;
}
}
object IHierarchyData.Item
{
get
{
return item;
}
}
public override string ToString()
{
return item[displayField].ToString();
}
string IHierarchyData.Path
{
get
{
string path = "/*[position()=1]";
GetPath(item, ref path);
return path;
}
}
void GetPath(DataRowView crow, ref string path)
{
foreach (DataRowView row in item.DataView)
{
string c, d;
c = crow.Row.ItemArray[2].ToString();
d = crow.Row.ItemArray[0].ToString();
string a = crow[dataParentIdField].ToString();
string b = row[dataIdField].ToString();
if (a == b)
{
path = "/*[position()=1]" + path;
}
}
}
string IHierarchyData.Type
{
get { return displayField; }
}
IHierarchicalEnumerable IHierarchyData.GetChildren()
{
InsHierarchicalEnumerable children = new InsHierarchicalEnumerable();
foreach (DataRowView row in item.DataView)
{
string a = row[dataParentIdField].ToString();
string b = item[dataIdField].ToString();
if (a == b)
children.Add(new InsHierarchyData(dataParentIdField, dataIdField,displayField, row));
}
return children;
}
IHierarchyData IHierarchyData.GetParent()
{
foreach (DataRowView row in item.DataView)
{
string a = item[dataParentIdField].ToString();
string b = row[dataIdField].ToString();
if (a == b)
return new InsHierarchyData(dataParentIdField, dataIdField,displayField, row);
}
return null;
}
System.ComponentModel.AttributeCollection ICustomTypeDescriptor.GetAttributes()
{
return TypeDescriptor.GetAttributes(this, true);
}
string ICustomTypeDescriptor.GetClassName()
{
return TypeDescriptor.GetClassName(this, true);
}
string ICustomTypeDescriptor.GetComponentName()
{
return TypeDescriptor.GetComponentName(this, true);
}
TypeConverter ICustomTypeDescriptor.GetConverter()
{
return TypeDescriptor.GetConverter(this, true);
}
EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(this, true);
}
PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(this, true);
}
object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(this, editorBaseType, true);
}
EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(this, attributes, true);
}
EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
{
return TypeDescriptor.GetEvents(this, true);
}
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
{
PropertyDescriptorCollection pds = TypeDescriptor.GetProperties(item);
if (pds.Count > 0)
{
List<InsHierarchyDataPropertyDescriptor> list = new List<InsHierarchyDataPropertyDescriptor>();
foreach (PropertyDescriptor pd in pds)
{
list.Add(new InsHierarchyDataPropertyDescriptor(pd.Name));
}
InsHierarchyDataPropertyDescriptor[] arr = new InsHierarchyDataPropertyDescriptor[list.Count];
list.CopyTo(arr);
return new PropertyDescriptorCollection(arr);
}
return PropertyDescriptorCollection.Empty;
}
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
{
return ((ICustomTypeDescriptor)this).GetProperties(null);
}
object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
{
if (pd is InsHierarchyDataPropertyDescriptor)
{
return this;
}
return null;
}
}
public class InsHierarchyDataPropertyDescriptor : PropertyDescriptor
{
private string name;
public InsHierarchyDataPropertyDescriptor(string name)
: base(name, null)
{
this.name = name;
}
public override string Name
{
get { return name; }
}
public override Type ComponentType
{
get
{
return typeof(InsHierarchyData);
}
}
public override bool IsReadOnly
{
get
{
return true;
}
}
public override Type PropertyType
{
get
{
return Type.GetType("System.String");
}
}
public override bool CanResetValue(object o)
{
return false;
}
public override object GetValue(object o)
{
InsHierarchyData shd = o as InsHierarchyData;
if (shd != null)
{
IHierarchyData hd = (IHierarchyData)shd;
string subject = ((DataRowView)(hd.Item))[name].ToString();
return subject;
}
return null;
}
public override void ResetValue(object o)
{
throw new NotSupportedException();
}
public override void SetValue(object o, object value)
{
throw new NotSupportedException();
}
public override bool ShouldSerializeValue(object o)
{
return true;
}
public override TypeConverter Converter
{
get { return TypeDescriptor.GetConverter(typeof(System.String)); }
}
}
public class InsHierarchicalEnumerable : ArrayList, IHierarchicalEnumerable
{
IHierarchyData IHierarchicalEnumerable.GetHierarchyData(object enumeratedItem)
{
return (InsHierarchyData)enumeratedItem;
}
}
public class InsHierarchicalDataSourceView : HierarchicalDataSourceView
{
string viewPath;
InsTreeViewDataSource owner;
public InsHierarchicalDataSourceView(InsTreeViewDataSource owner, string viewPath)
{
this.viewPath = viewPath;
this.owner = owner;
}
public override IHierarchicalEnumerable Select()
{
//DataView dv = (DataView)this.owner.Select(DataSourceSelectArguments.Empty);
DataView dv = (DataView)this.owner.DataView;
InsHierarchicalEnumerable data = new InsHierarchicalEnumerable();
bool hasParent = false;
foreach (DataRowView crow in dv)
{
object[] array1 = crow.Row.ItemArray;
hasParent = false;
foreach (DataRowView prow in dv)
{
object[] array2 = prow.Row.ItemArray;
//子节点
string a = crow[owner.DataParentIdField].ToString();
//根节点
string b = prow[owner.DataIdField].ToString();
if (a == b)
{
hasParent = true;
break;
}
}
//添加根节点
if (!hasParent)
data.Add(new InsHierarchyData(owner.DataParentIdField, owner.DataIdField, owner.DisplayField, crow));
}
return data;
}
}
public class InsTreeViewDataSource : IHierarchicalDataSource
{
private InsHierarchicalDataSourceView view = null;
private string parentIdField;
private string idField;
private DataTable dataSource;
private string displayField;
public InsTreeViewDataSource(string parentIdField, string idField, string displayField,DataTable dataSource)
{
this.parentIdField = parentIdField;
this.idField = idField;
this.displayField = displayField;
this.dataSource = dataSource;
}
public DataView DataView
{
get
{
return this.dataSource.DefaultView;
}
}
HierarchicalDataSourceView IHierarchicalDataSource.GetHierarchicalView(string viewPath)
{
if (null == this.view)
this.view = new InsHierarchicalDataSourceView(this, viewPath);
return this.view;
}
public string DataParentIdField
{
get { return parentIdField; }
set { parentIdField = value; }
}
public string DataIdField
{
get { return idField; }
set { idField = value; }
}
public string DisplayField
{
get { return displayField; }
set { displayField = value; }
}
public event EventHandler DataSourceChanged;
}
客户端代码:
DataTable dt = null;//自定义数据源
InsTreeViewDataSource dataSource = new InsTreeViewDataSource("Parent_Id", "ID", "FULL_NAME", dt);
this.itvTree.DataSource = dataSource;
this.itvTree.DataBind();
回复

使用道具 举报

3

主题

2万

回帖

172

积分

注册会员

Rank: 2

积分
172
发表于 2022-11-25 16:01:27 | 显示全部楼层
啦啦啦啦啦啦哈哈哈
回复 支持 反对

使用道具 举报

16

主题

2万

回帖

376

积分

中级会员

Rank: 3Rank: 3

积分
376
发表于 2023-3-21 01:26:25 | 显示全部楼层
飞飞飞飞飞飞飞飞飞飞飞飞飞
回复 支持 反对

使用道具 举报

1

主题

2万

回帖

155

积分

注册会员

Rank: 2

积分
155
发表于 2023-4-30 13:53:41 | 显示全部楼层
。。。。。。。。。。。。。。。
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

124

积分

注册会员

Rank: 2

积分
124
发表于 2024-2-28 16:15:25 | 显示全部楼层
啦啦啦啦啦啦啦啦!
回复 支持 反对

使用道具 举报

5

主题

2万

回帖

183

积分

注册会员

Rank: 2

积分
183
发表于 2024-4-2 01:28:02 | 显示全部楼层
这个源码还可以
回复 支持 反对

使用道具 举报

3

主题

1万

回帖

50

积分

注册会员

Rank: 2

积分
50
发表于 2024-5-8 15:59:55 | 显示全部楼层
搞个免费的用用
回复 支持 反对

使用道具 举报

7

主题

2万

回帖

288

积分

中级会员

Rank: 3Rank: 3

积分
288
发表于 2024-8-8 12:06:46 | 显示全部楼层
刷屏刷屏刷屏
回复 支持 反对

使用道具 举报

6

主题

2万

回帖

425

积分

中级会员

Rank: 3Rank: 3

积分
425
发表于 2024-9-4 13:32:13 | 显示全部楼层
。。。。。。。。。。。。。。。
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

115

积分

注册会员

Rank: 2

积分
115
发表于 2024-9-27 21:40:04 | 显示全部楼层
快更新啊,我擦
回复 支持 反对

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies

本版积分规则

手机版|小黑屋|网站地图|源码论坛 ( 海外版 )

GMT+8, 2024-11-24 01:57 , Processed in 0.067917 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表