Affichage de la Hiérarchie dans une Liste Déroulante

J'ai une hiérarchie des données que j'ai actuellement à l'écran dans un treeview. Je me demandais quelle serait la façon la plus facile de convertir cette hiérarchie dans une liste déroulante. Dans un treeview je peux trouver un nœud spécifique et ajouter un élément sous ce nœud. Je ne suis pas sûr de savoir comment le faire avec une liste déroulante. Ci-dessous le code que j'ai pour la liste déroulante jusqu'à présent:

Déroulant Hiérarchie
**Résolu

    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));
}
}
}
}
}
}
Ce n'est pas si loin?
Je suis en train de construire la hiérarchie, mais je suis un indice négatif. Je suis en train de faire quelque chose de similaire à ce que j'ai fait quand j'ai construit le treeview (code indiqué ci-dessus), en trouvant le "nœud" et placer le bon élément de la liste en-dessous.
L'élément est de vous donner l'indice négatif? En premier? Dernier? Tous?

OriginalL'auteur Will | 2011-06-06