This project demonstrates how to create a multi-column RadComboBox control dynamically at runtime.
There are two easy steps that you need to follow:
1. First add RadComboBox dynamically in the
Page.OnInit event handler.
2. Then set the
ItemTemplate and
HeaderTemplate properties to a new instance of the respective Template class:
[C#]
[VB.NET]
Please note that ItemTemplate and HeaderTemplate properties are of type ITemplate – that is why you need to create custom template classes that implement this interface.
To create a custom template class:
2.1. Create a new class that implements the
System.Web.UI.ITemplate interface:
[C#]
[VB.NET]
2.2. In the class, implement the
InstantiateIn method, which is a member of the ITemplate interface. This method provides a way to insert an instance of text and controls into the specified container.
2.3. In the InstantiateIn method, create the controls for the template item, set their properties, and then add them to the parent's Controls collection:
[C#]
[VB.NET]
2.4. Add a handler method for DataBinding event (highlighted above). In the handler, get the data that you want to bind to and assign it to the corresponding property of the control being bound:
[C#]
[VB.NET]
Private Sub tdPrice_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
Dim tdPrice As TableCell = DirectCast(sender, TableCell)
Dim item As RadComboBoxItem = DirectCast(tdPrice.BindingContainer, RadComboBoxItem)
Dim row As DataRowView = DirectCast(item.DataItem, DataRowView)
tdPrice.Text = row("UnitPrice").ToString()
End Sub
Private Sub tdQuantity_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
Dim tdQuantity As TableCell = DirectCast(sender, TableCell)
Dim item As RadComboBoxItem = DirectCast(tdQuantity.BindingContainer, RadComboBoxItem)
Dim row As DataRowView = DirectCast(item.DataItem, DataRowView)
tdQuantity.Text = row("UnitsInStock").ToString()
End Sub
Private Sub tdName_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
Dim tdName As TableCell = DirectCast(sender, TableCell)
Dim item As RadComboBoxItem = DirectCast(tdName.BindingContainer, RadComboBoxItem)
tdName.Text = item.Text
End Sub