Joel 2/23/2012 3:15:42 PM

Telerik - Grid with a combo box that repopulates based on the row type

This is a great piece of code that shows how to repopulate the data in a RadGrid dropdown list based on data in the row. In our example we have a grid of grocery items and the second column is a dropdown list:



But, when we're on a 'Fruit' row we only want to see 'Fruit' items in the ddl.


Make sense?

Imports Telerik.WinControls.UI
Public Class ComboBoxColumnThatChangesForEachRow
    Private Sub ComboBoxColumnThatChangesForEachRow_Load(sender As Object, e As System.EventArgsHandles Me.Load
        'set up the grid
        'populate the grid
        Me.RadGridView1.DataSource = getGridDataSet()
    End Sub
    Sub setupGrid()
        'get a reference to our grid class
        'this can be found here:
        Dim oTelerikGrid As New TelerikGrid
        'create the Produce Type column
        Dim oCol As GridViewTextBoxColumn = oTelerikGrid.createGridViewTextBoxColumn("Produce Type""ProduceType", 20, 100, "")
        oCol.ReadOnly = True
        'create the item column
        Dim oDT As DataTable = getFilteredDataSet("All")
        Me.RadGridView1.Columns.Add(oTelerikGrid.CreateGridViewComboBoxColumn("Type", oDT, "Item""Item""Item", 150))
        'set the grid properties
        Me.RadGridView1.ShowFilteringRow = False
        Me.RadGridView1.EnableGrouping = False
        Me.RadGridView1.AllowAddNewRow = False
        Me.RadGridView1.AllowDeleteRow = False
    End Sub
    Function getGridDataSet() As DataTable
        'returns a dataset that populates the grid
        Dim oDT As New DataTable
        Dim colType As DataColumn = New DataColumn("ProduceType", System.Type.GetType("System.String"))
        Dim colItem As DataColumn = New DataColumn("Item", System.Type.GetType("System.String"))
        Return oDT
    End Function
    Function getFilteredDataSet(ProduceType As StringAs DataTable
        'this dataset is used for the dropdown. 
        'it returns either all items, or a filtered list
        Dim oDT As New DataTable
        Dim colItem As DataColumn = New DataColumn("Item", System.Type.GetType("System.String"))
        Select Case ProduceType
            Case "Fruit"
            Case "Vegetable"
            Case "All"
        End Select
        Return oDT
    End Function
    Private Sub RadGridView1_CellEditorInitialized(sender As Object, e As Telerik.WinControls.UI.GridViewCellEventArgsHandles RadGridView1.CellEditorInitialized
        'this is the magic here. 
        'it repopulates the dropdown list based on the current row
            If e.Column.Name = "Item" Then
                Dim editor As RadDropDownListEditor
                editor = CType(Me.RadGridView1.ActiveEditor, RadDropDownListEditor)
                Dim editorElement As RadDropDownListEditorElement = CType(editor.EditorElement, RadDropDownListEditorElement)
                editorElement.DataSource = getFilteredDataSet(Me.RadGridView1.CurrentRow.Cells("ProduceType").Value)
            End If
        Catch ex As Exception
        End Try
    End Sub
End Class