
Imports System.Data
Imports Telerik.Web.UI

Partial Class DefaultVB
    Inherits System.Web.UI.Page

    Protected Sub cmbDepartment_DataBinding(sender As Object, e As EventArgs)
        Dim combo = CType(sender, RadComboBox)
        combo.DataSource = OrdersTable()
    End Sub



    Private Function OrdersTable() As DataTable
        Dim dt As New DataTable()

        dt.Columns.Add(New DataColumn("ShipCountry", Type.GetType("System.String")))

        dt.Rows.Add("Brazil")
        dt.Rows.Add("Belgium")
        dt.Rows.Add("Switzerland")
        dt.Rows.Add("Venezuela")
        dt.Rows.Add("DummyEntry")

        Return dt
    End Function


    Protected Sub RadGrid1_UpdateCommand(sender As Object, e As GridCommandEventArgs)
        Dim newValues As New Hashtable()
        Dim editedItem As GridEditableItem = TryCast(e.Item, GridEditableItem)
        e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem)
        Dim combo = CType(editedItem.FindControl("cmbDepartment"), RadComboBox)
        newValues.Add("ShipCountry", combo.Text.Replace("'", "''"))

        Dim parameterSets = New List(Of String)()

        For Each entry As DictionaryEntry In newValues
            parameterSets.Add(String.Format("{0}='{1}'", entry.Key, entry.Value))
        Next

        SqlDataSource1.UpdateCommand = String.Format("Update Orders SET {0} WHERE OrderID='{1}'",
           String.Join(", ", parameterSets.ToArray()), editedItem.GetDataKeyValue("OrderID")).Replace("=''", "=NULL")
        SqlDataSource1.Update()

    End Sub



    Protected Sub cmbDepartment_DataBound(sender As Object, e As EventArgs)
        Dim combo As RadComboBox = TryCast(sender, RadComboBox)
        Dim dataRowView As DataRowView = TryCast((TryCast(combo.NamingContainer, GridEditableItem)).DataItem, DataRowView)

        If dataRowView IsNot Nothing Then
            Dim existingIttem = combo.FindItemByValue(dataRowView(combo.DataValueField).ToString())
            If existingIttem IsNot Nothing Then
                existingIttem.Selected = True
            End If
        Else
            combo.Text = String.Empty
            combo.ClearSelection()
        End If
    End Sub
End Class
