TelerikDeveloper.com

TelerikDeveloper.com

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
        setupGrid()
 
        'populate the grid
        Me.RadGridView1.DataSource = getGridDataSet()
    End Sub
 
    Sub setupGrid()
        'get a reference to our grid class
        'this can be found here: http://dyndeveloper.com/thread.aspx?Threadid=1386
        Dim oTelerikGrid As New TelerikGrid
 
        'create the Produce Type column
        Dim oCol As GridViewTextBoxColumn = oTelerikGrid.createGridViewTextBoxColumn("Produce Type""ProduceType", 20, 100, "")
        oCol.ReadOnly = True
        Me.RadGridView1.Columns.Add(oCol)
 
        '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"))
        oDT.Columns.Add(colType)
        Dim colItem As DataColumn = New DataColumn("Item", System.Type.GetType("System.String"))
        oDT.Columns.Add(colItem)
 
        oDT.Rows.Add("Fruit""Bannana")
        oDT.Rows.Add("Fruit""Apple")
        oDT.Rows.Add("Vegetable""Squash")
        oDT.Rows.Add("Vegetable""Peas")
 
        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"))
        oDT.Columns.Add(colItem)
 
        Select Case ProduceType
            Case "Fruit"
                oDT.Rows.Add("Bannana")
                oDT.Rows.Add("Apple")
            Case "Vegetable"
                oDT.Rows.Add("Squash")
                oDT.Rows.Add("Peas")
            Case "All"
                oDT.Rows.Add("Bannana")
                oDT.Rows.Add("Apple")
                oDT.Rows.Add("Squash")
                oDT.Rows.Add("Peas")
        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
        Try
            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
            MsgBox(ex.Message)
        End Try
 
    End Sub
End Class