分层数据的数据源控件的实现,使用treeview控件显示无限级分类
1.数据结构如下
2.定义节点对象,实现IHierarchyData接口,此为重点实现,因为是分层结构,所以要重复在判断是否有子节点和MessageID和ParentID是否相等.当然具体情况具体分析了
3.定义节点对象集合,实现IHierarchicalEnumerable接口
4.定义视图,继承HierarchicalDataSourceView类
5.最后定义数据源控件,实现IHierarchicalDataSource接口
贴下代码
(1)
public class SqlHierarchyData : IHierarchyData, ICustomTypeDescriptor
{
private DataRowView item;
private string dataParentIdField;
private string dataIdField;
public SqlHierarchyData(string dataParentIdField, string dataIdField, DataRowView item)
{
this.item = item;
this.dataParentIdField = dataParentIdField;
this.dataIdField = dataIdField;
}
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();
HttpContext.Current.Response.Write(array1[2]+"-"+a + "--" +array2[2]+"-" +b + "<br>");
if (a == b)
return true;
}
return false;
}
}
object IHierarchyData.Item
{
get
{
return item;
}
}
public override string ToString()
{
return dataIdField;
}
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;
//GetPath(row, ref path);
//HttpContext.Current.Response.Write("begin<br>" + c + "--" + a + "--" + b + "<br>end<br>");
//HttpContext.Current.Response.Write(path + "<br>");
}
//HttpContext.Current.Response.Write(c + "--" + a + "--" +b + "<br>");
}
}
string IHierarchyData.Type
{
get { return dataIdField; }
}
//获取子节点
IHierarchicalEnumerable IHierarchyData.GetChildren()
{
SqlHierarchicalEnumerable children = new SqlHierarchicalEnumerable();
foreach (DataRowView row in item.DataView)
{
string a = row[dataParentIdField].ToString();
string b = item[dataIdField].ToString();
if (a == b)
children.Add(new SqlHierarchyData(dataParentIdField, dataIdField, 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 SqlHierarchyData(dataParentIdField, dataIdField, row);
}
return null;
}
ICustomTypeDescriptor Members#region ICustomTypeDescriptor Members
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<SqlHierarchyDataPropertyDescriptor> list = new List<SqlHierarchyDataPropertyDescriptor>();
foreach (PropertyDescriptor pd in pds)
{
list.Add(new SqlHierarchyDataPropertyDescriptor(pd.Name));
}
SqlHierarchyDataPropertyDescriptor[] arr = new SqlHierarchyDataPropertyDescriptor[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 SqlHierarchyDataPropertyDescriptor)
{
return this;
}
return null;
}
#endregion
}
public class SqlHierarchyDataPropertyDescriptor : PropertyDescriptor
{
private string name;
public SqlHierarchyDataPropertyDescriptor(string name)
: base(name, null)
{
this.name = name;
}
public override string Name
{
get { return name; }
}
public override Type ComponentType
{
get
{
return typeof(SqlHierarchyData);
}
}
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)
{
SqlHierarchyData shd = o as SqlHierarchyData;
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)); }
}
}
(2)
public class SqlHierarchicalEnumerable : ArrayList, IHierarchicalEnumerable
{
IHierarchyData IHierarchicalEnumerable.GetHierarchyData(object enumeratedItem)
{
return (SqlHierarchyData)enumeratedItem;
}
}
(3)
public class SqlHierarchicalDataSourceView : HierarchicalDataSourceView
{
string viewPath;
CustomSqlDataSource owner;
public SqlHierarchicalDataSourceView(CustomSqlDataSource owner, string viewPath)
{
this.viewPath = viewPath;
this.owner = owner;
}
public override IHierarchicalEnumerable Select()
{
DataView dv = (DataView)this.owner.Select(DataSourceSelectArguments.Empty);
SqlHierarchicalEnumerable data = new SqlHierarchicalEnumerable();
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 SqlHierarchyData(owner.DataParentIdField, owner.DataIdField, crow));
}
return data;
}
}
(4)
public class CustomSqlDataSource : SqlDataSource, IHierarchicalDataSource
{
private SqlHierarchicalDataSourceView view = null;
HierarchicalDataSourceView IHierarchicalDataSource.GetHierarchicalView(string viewPath)
{
if (null == this.view)
this.view = new SqlHierarchicalDataSourceView(this, viewPath);
return this.view;
}
private static readonly object EventKey = new object();
event EventHandler IHierarchicalDataSource.DataSourceChanged
{
add
{
Events.AddHandler(EventKey, value);
}
remove
{
Events.RemoveHandler(EventKey, value);
}
}
public string DataParentIdField
{
get { return ViewState["DataParentIdField"] != null ? (string)ViewState["DataParentIdField"] : string.Empty; }
set { ViewState["DataParentIdField"] = value; }
}
public string DataIdField
{
get { return ViewState["DataIdField"] != null ? (string)ViewState["DataIdField"] : string.Empty; }
set { ViewState["DataIdField"] = value; }
}
}
使用
<asp:TreeView runat="Server" ID="tv" DataSourceID="MySource">
<DataBindings>
<asp:TreeNodeBinding DataMember="MessageID" TextField="Subject" ValueField="MessageID" />
</DataBindings>
</asp:TreeView>
<custom:CustomSqlDataSource
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="Select * From Messages"
DataIdField="MessageID" DataParentIdField="ParentID" ID="MySource"
runat="server">
</custom:CustomSqlDataSource>
效果