HierarchicalDataSource

本文介绍了一种自定义分层数据源控件的实现方法,该方法利用SqlHierarchyData类实现了无限级分类数据的加载,并通过SqlHierarchicalDataSourceView和CustomSqlDataSource类实现了数据的层级展示。
       分层数据的数据源控件的实现,使用treeview控件显示无限级分类


1.数据结构如下



2.定义节点对象,实现IHierarchyData接口,此为重点实现,因为是分层结构,所以要重复在判断是否有子节点和MessageID和ParentID是否相等.当然具体情况具体分析了

3.定义节点对象集合,实现IHierarchicalEnumerable接口

4.定义视图,继承HierarchicalDataSourceView类

5.最后定义数据源控件,实现IHierarchicalDataSource接口

贴下代码

(1)
ContractedBlock.gifExpandedBlockStart.gif
None.gifpublic class SqlHierarchyData : IHierarchyData, ICustomTypeDescriptor
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif{
InBlock.gif    
private DataRowView item;
InBlock.gif    
private string dataParentIdField;
InBlock.gif    
private string dataIdField;
InBlock.gif
InBlock.gif    
public SqlHierarchyData(string dataParentIdField, string dataIdField, DataRowView item)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
this.item = item;
InBlock.gif      
this.dataParentIdField = dataParentIdField;
InBlock.gif      
this.dataIdField = dataIdField;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
bool IHierarchyData.HasChildren
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
get
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
foreach (DataRowView row in item.DataView)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
object[] array1 = row.Row.ItemArray;
InBlock.gif            
object[] array2 = item.Row.ItemArray;
InBlock.gif            
string a = row[dataParentIdField].ToString();
InBlock.gif            
string b = item[dataIdField].ToString();
InBlock.gif            HttpContext.Current.Response.Write(array1[
2]+"-"++ "--" +array2[2]+"-" ++ "<br>");
InBlock.gif            
if (a == b)
InBlock.gif            
return true;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
return false;
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
object IHierarchyData.Item
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
get
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
return item;
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public override string ToString()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
return dataIdField;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
string IHierarchyData.Path
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
get
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
string path = "/*[position()=1]";
InBlock.gif
InBlock.gif        GetPath(item, 
ref path);
InBlock.gif        
return path;
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif      
void GetPath(DataRowView crow, ref string path)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif          
InBlock.gif          
foreach (DataRowView row in item.DataView)
ExpandedSubBlockStart.gifContractedSubBlock.gif          
dot.gif{
InBlock.gif              
string c, d;
InBlock.gif              c 
= crow.Row.ItemArray[2].ToString();
InBlock.gif              d 
= crow.Row.ItemArray[0].ToString();
InBlock.gif              
string a = crow[dataParentIdField].ToString();
InBlock.gif              
string b = row[dataIdField].ToString();
InBlock.gif              
if (a == b)
ExpandedSubBlockStart.gifContractedSubBlock.gif              
dot.gif{
InBlock.gif                  path 
= "/*[position()=1]" + path;
InBlock.gif
InBlock.gif                  
//GetPath(row, ref path);
InBlock.gif                  
//HttpContext.Current.Response.Write("begin<br>" + c + "--" + a + "--" + b + "<br>end<br>");
InBlock.gif                  
//HttpContext.Current.Response.Write(path + "<br>");
ExpandedSubBlockEnd.gif
              }

InBlock.gif              
//HttpContext.Current.Response.Write(c + "--" + a + "--" +b + "<br>");
InBlock.gif

ExpandedSubBlockEnd.gif          }

ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif    
string IHierarchyData.Type
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif      
get dot.gifreturn dataIdField; }
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif      
//获取子节点
InBlock.gif
    IHierarchicalEnumerable IHierarchyData.GetChildren()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      SqlHierarchicalEnumerable children 
= new SqlHierarchicalEnumerable();
InBlock.gif
InBlock.gif      
foreach (DataRowView row in item.DataView)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif          
string a = row[dataParentIdField].ToString();
InBlock.gif          
string b = item[dataIdField].ToString();
InBlock.gif          
if (a == b)
InBlock.gif          children.Add(
new SqlHierarchyData(dataParentIdField, dataIdField, row));
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      
return children;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
//获取父节点
InBlock.gif
    IHierarchyData IHierarchyData.GetParent()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
foreach (DataRowView row in item.DataView)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string a = item[dataParentIdField].ToString();
InBlock.gif            
string b = row[dataIdField].ToString();
InBlock.gif            
if (a == b)
InBlock.gif                
return new SqlHierarchyData(dataParentIdField, dataIdField, row);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif      
return null;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
ICustomTypeDescriptor Members#region ICustomTypeDescriptor Members
InBlock.gif
InBlock.gif    System.ComponentModel.AttributeCollection ICustomTypeDescriptor.GetAttributes()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
return TypeDescriptor.GetAttributes(thistrue);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
string ICustomTypeDescriptor.GetClassName()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
return TypeDescriptor.GetClassName(thistrue);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
string ICustomTypeDescriptor.GetComponentName()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
return TypeDescriptor.GetComponentName(thistrue);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    TypeConverter ICustomTypeDescriptor.GetConverter()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
return TypeDescriptor.GetConverter(thistrue);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
return TypeDescriptor.GetDefaultEvent(thistrue);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
return TypeDescriptor.GetDefaultProperty(thistrue);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
return TypeDescriptor.GetEditor(this, editorBaseType, true);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
return TypeDescriptor.GetEvents(this, attributes, true);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
return TypeDescriptor.GetEvents(thistrue);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      PropertyDescriptorCollection pds 
= TypeDescriptor.GetProperties(item);
InBlock.gif
InBlock.gif      
if (pds.Count > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        List
<SqlHierarchyDataPropertyDescriptor> list = new List<SqlHierarchyDataPropertyDescriptor>();
InBlock.gif
InBlock.gif        
foreach (PropertyDescriptor pd in pds)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif          list.Add(
new SqlHierarchyDataPropertyDescriptor(pd.Name));
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        SqlHierarchyDataPropertyDescriptor[] arr 
= new SqlHierarchyDataPropertyDescriptor[list.Count];
InBlock.gif        list.CopyTo(arr);
InBlock.gif
InBlock.gif        
return new PropertyDescriptorCollection(arr);
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif
InBlock.gif      
return PropertyDescriptorCollection.Empty;
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
return ((ICustomTypeDescriptor)this).GetProperties(null);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
if (pd is SqlHierarchyDataPropertyDescriptor)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
return this;
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      
return null;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockEnd.gif    
#endregion

ExpandedBlockEnd.gif  }

None.gif
None.gif
public class SqlHierarchyDataPropertyDescriptor : PropertyDescriptor
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif{
InBlock.gif    
private string name;
InBlock.gif
InBlock.gif
InBlock.gif    
public SqlHierarchyDataPropertyDescriptor(string name)
InBlock.gif      : 
base(name, null)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
this.name = name;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public override string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif      
get dot.gifreturn name; }
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public override Type ComponentType
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
get
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
return typeof(SqlHierarchyData);
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public override bool IsReadOnly
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
get
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
return true;
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public override Type PropertyType
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
get
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
return Type.GetType("System.String");
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
InBlock.gif    
public override bool CanResetValue(object o)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
return false;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public override object GetValue(object o)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      SqlHierarchyData shd 
= o as SqlHierarchyData;
InBlock.gif
InBlock.gif      
if (shd != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        IHierarchyData hd 
= (IHierarchyData)shd;
InBlock.gif        
string subject = ((DataRowView)(hd.Item))[name].ToString();
InBlock.gif        
return subject;
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      
return null;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public override void ResetValue(object o)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
throw new NotSupportedException();
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
InBlock.gif    
public override void SetValue(object o, object value)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
throw new NotSupportedException();
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public override bool ShouldSerializeValue(object o)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
return true;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public override TypeConverter Converter
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif      
get dot.gifreturn TypeDescriptor.GetConverter(typeof(System.String)); }
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif  }

(2)
None.gif  public class SqlHierarchicalEnumerable : ArrayList, IHierarchicalEnumerable
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif{
InBlock.gif 
InBlock.gif    IHierarchyData IHierarchicalEnumerable.GetHierarchyData(
object enumeratedItem)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
return (SqlHierarchyData)enumeratedItem;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif  }

(3)
ContractedBlock.gifExpandedBlockStart.gif
None.gif public class SqlHierarchicalDataSourceView : HierarchicalDataSourceView
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif{
InBlock.gif    
string viewPath;
InBlock.gif    CustomSqlDataSource owner;
InBlock.gif
InBlock.gif
InBlock.gif    
public SqlHierarchicalDataSourceView(CustomSqlDataSource owner, string viewPath)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
this.viewPath = viewPath;
InBlock.gif      
this.owner = owner;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
InBlock.gif    
public override IHierarchicalEnumerable Select()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      DataView dv 
= (DataView)this.owner.Select(DataSourceSelectArguments.Empty);
InBlock.gif
InBlock.gif      SqlHierarchicalEnumerable data 
= new SqlHierarchicalEnumerable();
InBlock.gif
InBlock.gif      
bool hasParent = false;
InBlock.gif
InBlock.gif      
foreach (DataRowView crow in dv)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif          
object[] array1 = crow.Row.ItemArray;
InBlock.gif        hasParent 
= false;
InBlock.gif
InBlock.gif        
foreach (DataRowView prow in dv)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
object[] array2 = prow.Row.ItemArray;
InBlock.gif            
//子节点
InBlock.gif
            string a = crow[owner.DataParentIdField].ToString();
InBlock.gif            
//根节点
InBlock.gif
            string b = prow[owner.DataIdField].ToString();
InBlock.gif            
if (a == b)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                hasParent 
= true;
InBlock.gif                
break;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
//添加根节点
InBlock.gif
        if (!hasParent)
InBlock.gif          data.Add(
new SqlHierarchyData(owner.DataParentIdField, owner.DataIdField, crow));
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      
return data;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif  }

(4)

None.gif public class CustomSqlDataSource : SqlDataSource, IHierarchicalDataSource
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif{
InBlock.gif    
private SqlHierarchicalDataSourceView view = null;
InBlock.gif
InBlock.gif
InBlock.gif    HierarchicalDataSourceView IHierarchicalDataSource.GetHierarchicalView(
string viewPath)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
if (null == this.view)
InBlock.gif        
this.view = new SqlHierarchicalDataSourceView(this, viewPath);
InBlock.gif
InBlock.gif      
return this.view;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
private static readonly object EventKey = new object();
InBlock.gif
InBlock.gif    
event EventHandler IHierarchicalDataSource.DataSourceChanged
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      add
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        Events.AddHandler(EventKey, value);
ExpandedSubBlockEnd.gif      }

InBlock.gif      remove
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        Events.RemoveHandler(EventKey, value);
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
InBlock.gif    
public string DataParentIdField
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif      
get dot.gifreturn ViewState["DataParentIdField"!= null ? (string)ViewState["DataParentIdField"] : string.Empty; }
ExpandedSubBlockStart.gifContractedSubBlock.gif      
set dot.gif{ ViewState["DataParentIdField"= value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public string DataIdField
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif      
get dot.gifreturn ViewState["DataIdField"!= null ? (string)ViewState["DataIdField"] : string.Empty; }
ExpandedSubBlockStart.gifContractedSubBlock.gif      
set dot.gif{ ViewState["DataIdField"= value; }
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif  }


使用

None.gif   <asp:TreeView runat="Server" ID="tv" DataSourceID="MySource">
None.gif      
<DataBindings>
None.gif        
<asp:TreeNodeBinding DataMember="MessageID" TextField="Subject" ValueField="MessageID" />
None.gif      
</DataBindings>
None.gif    
</asp:TreeView>
None.gif    
<custom:CustomSqlDataSource 
None.gif    
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
None.gif      SelectCommand
="Select * From Messages"
None.gif      DataIdField
="MessageID" DataParentIdField="ParentID" ID="MySource"
None.gif      runat
="server">
None.gif    
</custom:CustomSqlDataSource>

效果
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值