在DropDown列表中显示层次结构

时间:2011-06-06 12:33:30

标签: c# asp.net tsql drop-down-menu hierarchy

我有一个数据层次结构,我目前在树视图中显示。我想知道将此层次结构转换为下拉列表的最简单方法是什么。在树视图中,我可以找到特定节点并在该节点下添加项目。我不确定如何使用下拉列表执行此操作。下面是我到目前为止下拉列表的代码:

DropDown Hierarchy
** 解决

    public void DropDownTree(DropDownList ddl)
    {
        ddl.Items.Clear(); 
        using (SqlConnection connection = new SqlConnection())
        {
            // Data Connection
            connection.ConnectionString = (ConfigurationManager.ConnectionStrings["AssetWhereConnectionString"].ConnectionString);
            connection.Open();
            string getLocations = @"
                With hierarchy (id, [location id], name, depth, [path])
                As (

                    Select ID, [LocationID], Name, 1 As depth,
                        Cast(Null as varChar(max)) As [path]
                    From dbo.Locations
                    Where ID = [LocationID]

                    Union All

                    Select child.id, child.[LocationID], child.name,
                        parent.depth + 1 As depth,
                        IsNull(
                            Cast(parent.id As varChar(max)),
                            Cast(parent.id As varChar(max))
                        ) As [path]
                    From dbo.Locations As child
                    Inner Join hierarchy As parent
                        On child.[LocationID] = parent.ID
                    Where child.ID != parent.[Location ID])

                Select *
                From hierarchy
                Order By [depth] Asc";

            using (SqlCommand cmd = new SqlCommand(getLocations, connection))
            {
                cmd.CommandType = CommandType.Text;
                using (SqlDataReader rs = cmd.ExecuteReader())
                {
                    while (rs.Read())
                    {
                        string id = rs.GetGuid(0).ToString();
                        int depth = rs.GetInt32(3);
                        string text = rs.GetString(2);
                        string locationID = rs.GetGuid(1).ToString();
                        string padding = String.Concat(Enumerable.Repeat("- ", 2 * depth));


                        if (id == locationID)
                        {
                            ddl.Items.Add(new ListItem(padding + text, id));
                        }
                        else
                        {
                            int index = ddl.Items.IndexOf(ddl.Items.FindByValue(rs.GetString(4).ToString().ToLower()));

                            // Fix the location where the item is inserted. 
                            index = index + 1;

                            ddl.Items.Insert(index, new ListItem(padding + text, id));

                        }
                    }
                }
            }
        }
    }

3 个答案:

答案 0 :(得分:3)

您的代码看起来不错,但根据您提问时的评论,听起来您在这一行上的索引值为负值:

int index =  ddl.Items.IndexOf(ddl.Items.FindByValue(rs.GetString(4).ToString().ToLower()));

否定值表示找不到您要搜索的下拉列表项。如果下拉列表为空,肯定会出现这种情况。你会搜索一个不存在的项目。

以下是对您的代码的更新,它将产生以下内容(我相信您正在寻找的内容): enter image description here

尝试将代码更改为以下内容:

if (id == locationID)
{
    ddl.Items.Add(new ListItem(padding + text, id));
}
else
{
    int index =  ddl.Items.IndexOf(ddl.Items.FindByValue(rs.GetString(4).ToString().ToLower()));

    //Check to see if this item exists before trying to insert
    if (index == -1) 
    {
        //Add the item if it doesn't exist
        ddl.Items.Add(new ListItem(padding + text, id));
    }
    else
    {
        ddl.Items.Insert(index, new ListItem(padding + text, id));
    }
}

答案 1 :(得分:1)

你唯一的方法是将一个字符串连接到节点描述:它可能是一个空白区域,只是为了将子节点与其父节点缩进或以图形方式更好地

答案 2 :(得分:1)

使用标准HTML选择列表,有两种显示层次结构的方法:

  • 如果无法选择根节点,则可以使用 optgroup元素。不幸的是,ASP.NET DropDownList不支持 optgroups。

  • 如果可以选择根节点,请使用 用于缩进子节点的空格

我想说最简单的方法是在结果集中使用深度值:

    DropDownList ddl = new DropDownList();
    while (rs.Read())
    {
        string id = rs.GetGuid(0).ToString();
        int depth = rs.GetInt32(3);
        string text = rs.GetString(2);
        string padding = String.Concat(Enumerable.Repeat(" ", 4 * depth));
        ddl.Items.Add(new ListItem(padding + text, id));
    }
相关问题