AUTHOR: Peter Milchev
DATE POSTED: January 16, 2019
Grouping of DropDownList similar to RadComboBox - https://docs.telerik.com/devtools/aspnet-ajax/controls/combobox/how-to/grouping-items
Sort the datasource by the GroupBy property(in the example below, the GroupBy property is Country using the Northwind database's Customers table);
<style>
.separator{
font-style
:
italic
;
border-top
2px
solid
darkblue;
border-bottom
1px
grey;
}
</style>
<
telerik:RadDropDownList
ID
=
"RadDropDownList1"
runat
"server"
RenderMode
"Lightweight"
DataSourceID
"SqlDataSource1"
DataTextField
"ContactName"
DataValueField
"CustomerID"
OnDataBound
"RadDropDownList1_DataBound"
OnItemDataBound
"RadDropDownList1_ItemDataBound"
>
</
asp:SqlDataSource
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT [CustomerID], [ContactName], [ContactTitle], [CompanyName], [Country] FROM [Customers] order by [Country]"></
// Country is our GroupBy property
// Countries is our used GroupBy properties collection
public
List<
string
> Countries =
new
>();
protected
void
RadDropDownList1_ItemDataBound(
object
sender, Telerik.Web.UI.DropDownListItemEventArgs e)
{
var dataitem = e.Item.DataItem
as
DataRowView;
if
(dataitem !=
null
)
(dataitem[
"Country"
] !=
DBNull.Value
var country = dataitem[
].ToString();
e.Item.Attributes.Add(
, country);
else
,
"Unknown"
);
RadDropDownList1_DataBound(
sender, EventArgs e)
var allItems = RadDropDownList1.Items;
for
(
int
i = 0; i < RadDropDownList1.Items.Count; i++)
var item = RadDropDownList1.Items[i];
var country = item.Attributes[
];
(!Countries.Contains(country))
Countries.Add(country);
var separatorItem =
DropDownListItem() { Text = country, CssClass =
"separator"
, Enabled =
false
};
RadDropDownList1.Items.Insert(item.Index, separatorItem);
i++;
Resources Buy Try