Show hierarchy in dropdown list

I have a data hierarchy, which I now show in a tree. I was wondering what would be the easiest way to convert this hierarchy into a drop-down list. In the tree structure, I can find a specific node and add an element under that node. I'm not sure how to do this with a drop down list. Below is the code for the drop down list:

DropDown Hierarchy
** Solvable

    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
source share
3 answers

Your code looks great, but from your comment on your question, it seems that you are getting a negative value for the index on this line:

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

, , - Node : , Node -

+1

HTML :

  • , optgroup. , ASP.NET DropDownList optgroups.

  • If root nodes can be selected, use whitespace for child indent nodes

I would say that the easiest way is to use the depth value in your result set:

    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));
    }
+1
source

All Articles