Imports Telerik.Web.UI
Imports System.Data

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Init(sender As Object, e As System.EventArgs) Handles Me.Init
        Dim grid As New RadGrid()
        grid.AutoGenerateColumns = False
        grid.DataSourceID = "SqlDataSource1"
        Dim boundColumn1 As New GridBoundColumn()
        boundColumn1.DataField = "ContactName"
        boundColumn1.UniqueName = "ConactName"
        boundColumn1.HeaderText = "Bound Column"
        grid.MasterTableView.Columns.Add(boundColumn1)
        Dim templateColumnName As String = "City"
        Dim templateColumn As New GridTemplateColumn()
        templateColumn.ItemTemplate = New MyTemplate(templateColumnName)
        templateColumn.HeaderText = templateColumnName
        grid.MasterTableView.Columns.Add(templateColumn)
        templateColumnName = "Country"
        templateColumn = New GridTemplateColumn()
        templateColumn.ItemTemplate = New MyTemplate(templateColumnName)
        templateColumn.HeaderText = templateColumnName
        grid.MasterTableView.Columns.Add(templateColumn)
        grid.AllowPaging = True
        Panel1.Controls.Add(grid)
    End Sub
    Private Class MyTemplate
        Implements ITemplate
        Protected ddl As DropDownList
        Protected lbl As Label
        Private colname As String
        Public Sub New(ByVal cName As String)
            colname = cName
        End Sub
        Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements ITemplate.InstantiateIn
            ddl = New DropDownList()
            ddl.ID = colname + "_templateColumnDropDownList"
            ddl.AutoPostBack = True
            AddHandler ddl.DataBinding, AddressOf ddl_DataBinding
            AddHandler ddl.SelectedIndexChanged, AddressOf ddl_SelectedIndexChanged
            container.Controls.Add(ddl)
            lbl = New Label()
            lbl.ID = colname + "_templateColumnLabel"
            container.Controls.Add(lbl)
        End Sub
        Sub ddl_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
            Dim ddl As DropDownList = DirectCast(sender, DropDownList)
            ddl.DataSourceID = "SqlDataSource1"
            ddl.DataTextField = colname
            ddl.DataValueField = colname
            Dim container As GridDataItem = DirectCast(ddl.NamingContainer, GridDataItem)
            ddl.SelectedValue = (DirectCast(container.DataItem, DataRowView))(colname).ToString()
        End Sub
        Public Sub ddl_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
            Dim ddl As DropDownList = DirectCast(sender, DropDownList)
            Dim container As GridDataItem = DirectCast(ddl.NamingContainer, GridDataItem)
            Dim lbl As Label = CType(container.FindControl(colname + "_templateColumnLabel"), Label)
            lbl.Text = ddl.SelectedValue
        End Sub
    End Class
End Class
