Welcome to AspAdvice Sign in | Join | Help

AzamSharp

Some day I will know everything I hope that day never comes

Syndication

Tags

Navigation

Displaying Hierarchical Data in DropDownList

Sometimes you need to view the hierarchical data in the DropDownList. Something like the following:

Category 1

Product 1

Product 2

Category 2

Product 3

Product 10

and like that. Here is a simple code that shows the hierarchical data in the DropDownList. I am using the Northwind database.

private void BindData()

{

string connectionString = "Server=localhost;Database=Northwind;Trusted_Connection=true";

SqlConnection myConnection = new SqlConnection(connectionString);

SqlCommand myCommand = new SqlCommand("usp_GetProductsForCategories",myConnection);

myCommand.CommandType = CommandType.StoredProcedure;

SqlDataAdapter ad = new SqlDataAdapter(myCommand);

DataSet ds = new DataSet();

ad.Fill(ds);

foreach (DataRow row in ds.Tables[0].Rows)

{

int categoryID = Convert.ToInt32(row["CategoryID"]);

string categoryName = row["CategoryName"] as String;

 

ddlCategories.Items.Add(new ListItem( categoryName , categoryID.ToString()));

DataRow[] childRows = ds.Tables[1].Select("CategoryID = " + categoryID);

foreach (DataRow childRow in childRows)

{

ddlCategories.Items.Add(new ListItem( (string) childRow["ProductName"], (childRow["ProductID"].ToString())));

}

}

// bind the dropdownlist

ddlCategories.DataBind();

}

Published Wednesday, December 06, 2006 12:07 AM by azamsharp

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Displaying Hierarchical Data in DropDownList @ Wednesday, December 06, 2006 10:01 PM

you could create a custom dropdownlist that supports the optgroup option, that way you'll see the indent after each group

m1

# re: Displaying Hierarchical Data in DropDownList @ Wednesday, December 06, 2006 10:07 PM

Thanks M1. Do you have any references for the custom dropDownList?

Indentation is one of the key features missing in the hierarchical dropdownlist.

azamsharp

# re: Displaying Hierarchical Data in DropDownList @ Tuesday, December 26, 2006 11:25 PM

There are two ways to add indentation: VB.NET ==== Dim padding As String = Chr(160) & Chr(160) ddl.Items.Add(New ListItem(padding & "Hello World","123")) C# === string padding = "\xA0\xA0"; ddlItems.Add(new ListItem(padding + "Hello World","123")); Or set it in the data source VB.NET ==== Dim table As New DataTable table.Columns.Add("Value", GetType(String)) table.Columns.Add("Text", GetType(String)) Dim padding As String = Chr(160) & Chr(160) table.Rows.Add(New Object() {padding & "123", "123"}) ddl.DataValueField = "Field" ddl.DataTextField = "Text" ddl.DataSource = table ddl.DataBind() C# === DataTable table = new DataTable(); table.Columns.Add("Value", typeof(string)); table.Columns.Add("Text", typeof(string)); string padding = "\xA0\xA0"; table.Rows.Add(new object [] {padding + "123","123"}); ddl.DataValueField = "Field"; ddl.DataTextField = "Text"; ddl.DataSource = table; ddl.DataBind(); Another thing ? How can you tell where the value is from after a postback ? Is it the category_id or product_id ?

MrFlo

# re: Displaying Hierarchical Data in DropDownList @ Wednesday, December 27, 2006 2:21 PM

Thanks for the comment. Since, you are more interested in the productID rather then the categoryID we can make the categoryID = "0". And then check if the selectedID is not zero which means that it is a productID.

protected void Button1_Click(object sender, EventArgs e)

   {

       string selectedValue = String.Empty;

       if (Page.IsValid)

       {

           selectedValue = ddlCategories.SelectedValue;

           if (!String.IsNullOrEmpty(selectedValue) && selectedValue.Equals("0") == false)

           {

               lblMessage.Text = "Product is selected";              

           }

           else

           {

               lblMessage.Text = "Please select a product";

           }

       }

   }

azamsharp

# re: Displaying Hierarchical Data in DropDownList @ Wednesday, December 27, 2006 4:07 PM

Here is a link to my latest article on this issue:

http://gridviewguy.com/ArticleDetails.aspx?articleID=227

Enjoy!

azamsharp

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below