在下拉列表中显示层级结构

3

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

下拉层次结构
**已解决

    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));

                        }
                    }
                }
            }
        }
    }

我正在尝试构建层次结构,但是我得到了一个负索引。我正在尝试做类似于构建树视图时所做的事情(上面列出了该代码),通过查找“节点”并将正确的列表项放置在其下方。 - Will
哪个项给了你负索引?第一个?最后一个?全部? - NakedBrunch
3个回答

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));
    }
}

您对我的问题是正确的。我之所以会遇到这个问题,是因为在某个点上我的路径(GetString(4))与任何值都不匹配。一旦路径包含“/”,我就开始得到-1。是否有一种方法可以将路径中的最后一个值去掉?例如:如果我有 abc/def/ghi,如何仅获取“/”后面的最后一个值(“ghi”)? - Will
没事了,我通过编辑 SQL 解决了问题。我还必须将索引加一,以便层次结构向下构建而不是向上。看起来我的问题已经解决了。我会在上面更新我的代码并附上解决方案。非常感谢你的帮助! - Will

1
你唯一的方法是将一个字符串连接到节点描述中:它可能是一个空格,只是为了使子节点相对于其父节点缩进或者在图形上更好看。

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));
    }

这确实成功地将值分隔开并将它们添加到下拉列表中,但不幸的是它没有保留层次结构。我不确定如何保留层次结构。 - Will
我尝试通过查找索引来复制查找下拉列表的子节点。它几乎可以工作,但是我得到了一个负索引。我已经在上面编辑了我的代码,展示了我目前拥有的内容。 - Will

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接