C# RadTreeNode根据用户登录重新排序,然后按字母顺序排序
请让我知道我是否可以实现基于以下场景 因此,如果用户以domain/dan.campbell登录,那么我需要按照以下方式对RadTreeView进行排序: 坎贝尔,丹C# RadTreeNode根据用户登录重新排序,然后按字母顺序排序,c#,asp.net,radtreeview,C#,Asp.net,Radtreeview,请让我知道我是否可以实现基于以下场景 因此,如果用户以domain/dan.campbell登录,那么我需要按照以下方式对RadTreeView进行排序: 坎贝尔,丹 耐克 美洲狮 安斯顿,埃里克 阿迪达斯 美洲狮 布莱恩,埃里克 阿迪达斯 美洲狮 根据以下代码,当前树显示如下所示: 安斯顿,埃里克 阿迪达斯 美洲狮 布莱恩,埃里克 阿迪达斯 美洲狮 坎贝尔,丹 耐克 美洲狮 受保护的无效页面加载(对象发送方、事件参数e) { BuildTree(); SortClass(
- 耐克
- 美洲狮
- 阿迪达斯
- 美洲狮
- 阿迪达斯
- 美洲狮
- 阿迪达斯
- 美洲狮
- 阿迪达斯
- 美洲狮
- 耐克
- 美洲狮
受保护的无效页面加载(对象发送方、事件参数e)
{
BuildTree();
SortClass();
}
公共void BuildTree() { EntityCollection集合=GetProduct(); treProduct.Nodes.Clear()//Sort all RadTreeNode in Ascending public void SortClass() { SortNodes(treProduct.Nodes); }
ArrayList pgnodes=new ArrayList(); RadTreeNode pnode=null; RadTreeNode snode=null; foreach(集合中的ProductEntity p) { pnode=null; foreach(pgnodes中的RadTreeNode n) { 如果(n.Text.Trim()==p.LastName.Trim().ToUpper()+”,“+”+p.FirstName.Trim().ToUpper()) { pnode=n; 打破 } } if(pnode!=null) { RadTreeNode productNode=新的RadTreeNode(p.ProductName.toString()); pnode.nodes.Add(productNode); } 其他的 { RadTreeNode userNode=新的RadTreeNode(p.LastName.Trim().ToUpper()+,“+”+p.FirstName.Trim().ToUpper()); RadTreeNode productNode=新的RadTreeNode(p.ProductName.toString()); userNode.Nodes.Add(productNode); pgnodes.Add(userNode); } } foreach(pgnodes中的RadTreeNode pg) { treProduct.Nodes.Add(pg); } treProduct.CollapseAllNode(); } /// ///对子节点进行排序的每个节点级别都会调用排序节点 /// /// 公共void排序(RadTreeNodeCollection集合) { RadTreeNode[]节点=新的RadTreeNode[collection.Count]; collection.CopyTo(节点,0); Sort(nodes,new NodeSorter()); collection.Clear(); collection.AddRange(节点); } /// ///SortNodes是一种对所有区域进行枚举和排序的递归方法 /// /// 私有无效SortNodes(RadTreeNodeCollection集合) { 分类(收集); foreach(集合中的RadTreeNode节点) { 如果(node.Nodes.Count>0) { SortNodes(node.Nodes); } } } /// ///TreeNodeCare定义排序标准 /// 公共类节点所有者:IComparer { 公共整数比较(对象x、对象y) { 放射性核素tx=(放射性核素)x; RadTreeNode ty=(RadTreeNode)y; //如果(tx.Text.Length!=ty.Text.Length) //返回tx.Text.Length-ty.Text.Length; 返回string.Compare(tx.Text,ty.Text); } }
公共字符串名
{
得到
{
字符串firstName=null;
字符串用户名=System.Security.Principal.WindowsIdentity.GetCurrent().Name.Substring(1);
字符串[]loginNameParts=userName.Split('\\');
字符串loginNameWithoutDomain=loginNameParts[1];
var name=loginNameWithoutDomain.Split('.');
firstName=名称[0];
返回名字;
}
}
公共字符串姓氏
{
得到
{
字符串lastName=null;
字符串用户名=System.Security.Principal.WindowsIdentity.GetCurrent().Name.Substring(1);
字符串[]loginNameParts=userName.Split('\\');
字符串loginNameWithoutDomain=loginNameParts[1];
var name=loginNameWithoutDomain.Split('.');
lastName=名称[1];
返回姓氏;
}
}
public void SortClass()
{
SortNodes(treProduct.Nodes);
字符串nameToFind=LastName.Trim().ToUpper()+,“+”+FirstName.Trim().ToUpper();
var node=treProduct.FindNodeByText(nameToFind);
如果(节点!=null)
{
node.Remove();
treProduct.Nodes.Insert(0,节点);
treProduct.Nodes[0]。Expanded=true;
treProduct.Nodes[0]。ExpandChildNodes();
}
}
公共void排序(RadTreeNodeCollection集合)
{
RadTreeNode[]节点=新的RadTreeNode[collection.Count];
collection.CopyTo(节点,0);
Sort(nodes,new NodeSorter());
collection.Clear();
collection.AddRange(节点);
}
///
///SortNodes是一种对所有区域进行枚举和排序的递归方法
///
///
私有无效SortNodes(RadTreeNodeCollection集合)
{
分类(收集);
foreach(集合中的RadTreeNode节点)
{
如果(node.Nodes.Count>0)
{
SortNodes(node.Nodes);
}
}
}
///
///TreeNodeCare定义排序标准
///
公共类节点所有者:IComparer
{
公共整数比较(对象x、对象y)
{
放射性核素tx=(放射性核素)x;
RadTreeNode ty=(RadTreeNode)y;
//如果(tx.Text.Length!=ty.Text.Length)
//返回tx.Text.Length-ty.Text.Length;
返回string.Compare(tx.Text,ty.Text);
}
}
ArrayList pgnodes = new ArrayList();
RadTreeNode pnode = null;
RadTreeNode snode = null;
foreach (ProductEntity p in collection)
{
pnode = null;
foreach(RadTreeNode n in pgnodes)
{
if(n.Text.Trim() == p.LastName.Trim().ToUpper() + "," +" " + p.FirstName.Trim().ToUpper())
{
pnode = n;
break;
}
}
if(pnode != null)
{
RadTreeNode productNode = new RadTreeNode(p.ProductName.toString());
pnode.nodes.Add(productNode);
}
else
{
RadTreeNode userNode = new RadTreeNode(p.LastName.Trim().ToUpper() + "," +" " + p.FirstName.Trim().ToUpper());
RadTreeNode productNode = new RadTreeNode(p.ProductName.toString());
userNode.Nodes.Add(productNode);
pgnodes.Add(userNode);
}
}
foreach(RadTreeNode pg in pgnodes)
{
treProduct.Nodes.Add(pg);
}
treProduct.CollapseAllNode();
}
/// <summary>
/// The sort node is called for each node level sorting the child node
/// </summary>
/// <param name="collection"></param>
public void Sort(RadTreeNodeCollection collection)
{
RadTreeNode[] nodes = new RadTreeNode[collection.Count];
collection.CopyTo(nodes, 0);
Array.Sort(nodes, new NodeSorter());
collection.Clear();
collection.AddRange(nodes);
}
/// <summary>
/// SortNodes is a recursive method enumarating and sorting all area
/// </summary>
/// <param name="collection"></param>
private void SortNodes(RadTreeNodeCollection collection)
{
Sort(collection);
foreach (RadTreeNode node in collection)
{
if (node.Nodes.Count > 0)
{
SortNodes(node.Nodes);
}
}
}
/// <summary>
/// TreeNodeCOmpare define the sorting criteria
/// </summary>
public class NodeSorter : IComparer
{
public int Compare(object x, object y)
{
RadTreeNode tx = (RadTreeNode)x;
RadTreeNode ty = (RadTreeNode)y;
//if (tx.Text.Length != ty.Text.Length)
// return tx.Text.Length - ty.Text.Length;
return string.Compare(tx.Text, ty.Text);
}
}
public String FirstName
{
get
{
string firstName = null;
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name.Substring(1);
string[] loginNameParts = userName.Split('\\');
string loginNameWithoutDomain = loginNameParts[1];
var names = loginNameWithoutDomain.Split('.');
firstName = names[0];
return firstName;
}
}
public String LastName
{
get
{
string lastName = null;
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name.Substring(1);
string[] loginNameParts = userName.Split('\\');
string loginNameWithoutDomain = loginNameParts[1];
var names = loginNameWithoutDomain.Split('.');
lastName = names[1];
return lastName;
}
}
public void SortClass()
{
SortNodes(treProduct.Nodes);
string nameToFind = LastName.Trim().ToUpper() + "," + " " + FirstName.Trim().ToUpper();
var node = treProduct.FindNodeByText(nameToFind);
if (node != null)
{
node.Remove();
treProduct.Nodes.Insert(0, node);
treProduct.Nodes[0].Expanded = true;
treProduct.Nodes[0].ExpandChildNodes();
}
}
public void Sort(RadTreeNodeCollection collection)
{
RadTreeNode[] nodes = new RadTreeNode[collection.Count];
collection.CopyTo(nodes, 0);
Array.Sort(nodes, new NodeSorter());
collection.Clear();
collection.AddRange(nodes);
}
/// <summary>
/// SortNodes is a recursive method enumarating and sorting all area
/// </summary>
/// <param name="collection"></param>
private void SortNodes(RadTreeNodeCollection collection)
{
Sort(collection);
foreach (RadTreeNode node in collection)
{
if (node.Nodes.Count > 0)
{
SortNodes(node.Nodes);
}
}
}
/// <summary>
/// TreeNodeCOmpare define the sorting criteria
/// </summary>
public class NodeSorter : IComparer
{
public int Compare(object x, object y)
{
RadTreeNode tx = (RadTreeNode)x;
RadTreeNode ty = (RadTreeNode)y;
//if (tx.Text.Length != ty.Text.Length)
// return tx.Text.Length - ty.Text.Length;
return string.Compare(tx.Text, ty.Text);
}
}