TreeView创建IHierarchicalDataSource类型的数据源实现
程序员文章站
2023-11-18 19:11:40
ihierarchicaldatasource实现: public class inshierarchydata : ihierarchydata, icustomtype...
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();
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();