This grid markup shows both column types side-by-side. Product Group uses a GridTemplateColumn, while SLPRSNID uses a GridDropDownColumn. As you can see, the TemplateColumn requires significantly more setup, wiring, and manual binding.
A short note about the markup before we start. If value field and the data field are different, You have to do this markup, and both fields need to be in the main data set. That seems a little awkward, but that's what it seems like they're doing
<telerik:GridDropDownColumn HeaderText="Contact"
DataField="ContactID"
ListValueField="ContactID"
ListDataMember="ContactName"
ListTextField="ContactName"
>
</telerik:GridDropDownColumn>
There are two events you must handle for either approach to work correctly: ItemDataBound and UpdateCommand.
<telerik:RadGrid ID="RadGrid2"
runat="server"
AutoGenerateColumns="false" MasterTableView-CommandItemSettings-ShowAddNewRecordButton="false"
AllowSorting="true"
AllowMultiRowSelection="true"
ClientSettings-Selecting-AllowRowSelect="true">
<MasterTableView DataKeyNames="RowID, TrackingNo, BidderID, ProductGroup, SLPRSNID" >
<Columns>
<telerik:GridTemplateColumn HeaderText="Product Group" DataField="ProductGroup" UniqueName="ProductGroup">
<ItemTemplate>
<%# Eval("ProductGroup") %>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadDropDownList ID="ddlProductGroup" runat="server" AutoPostBack="true"
DataTextField="ProductGroup"
DataValueField="ProductGroup"
Width="200px">
</telerik:RadDropDownList>
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridDropDownColumn HeaderText="Sales Rep" DataField="slprsnid"
ListTextField="slprsnid"
ListDataMember = "slprsnid"
ListValueField="slprsnid">
</telerik:GridDropDownColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
ItemDataBound
This event fires for every row as it’s being bound. The logic is similar for both column types, but the TemplateColumn requires more manual work:
The GridDropDownColumn, on the other hand, exposes a built-in editor (GridDropDownListColumnEditor), which simplifies access to the underlying RadComboBox. You still bind the data source and set the selected value, but the control discovery is cleaner.
Private Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs) Handles RadGrid1.ItemDataBound
Dim strError As String = ""
Try
'fires for each row that is bound to the grid.
If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then
'we are in edit mode, so we need to populate the dropdowns
Dim editItem As GridEditableItem = CType(e.Item, GridEditableItem)
Dim editMan As GridEditManager = editItem.EditManager
'get the current values
Dim strSalesperson As String = Convert.ToString(editItem.GetDataKeyValue("SLPRSNID"))
Dim strProductGroup As String = Convert.ToString(editItem.GetDataKeyValue("ProductGroup"))
'======================================================
'handle PRODUCT GROUP
'======================================================
'get a reference to the dropdown
Dim ddl As RadDropDownList = CType(editItem.FindControl("ddlProductGroup"), RadDropDownList)
'set the datasource
ddl.DataSource = SPs.fp_PTProductGroup_SEL_cbo(App.ConnectionString).getTable
ddl.DataBind()
'Preselect the current value
ddl.SelectedValue = strProductGroup.ToString()
'======================================================
'handle SLPRSNID
'======================================================
'get a reference to the dropdown
Dim editor As GridDropDownListColumnEditor = CType(editMan.GetColumnEditor("SLPRSNID"), GridDropDownListColumnEditor)
Dim ddlSalesperson As RadComboBox = editor.ComboBoxControl
'set the datasource
ddlSalesperson.DataSource = SPs.fp_RM00301_SEL_cbo5("acct", False, False, False, False, False, "cgunter", App.ConnectionString).getTable
ddlSalesperson.DataBind()
'Preselect the current value
ddlSalesperson.SelectedValue = strSalesperson
End If
Catch ex As Threading.ThreadAbortException
Catch ex As Exception
ErrorHandler.globalErrorHandler(ex, strError, App.AppName, App.UserName, False)
Me.lblError.Text = ex.Message
End Try
End Sub
UpdateCommand
The update logic is also comparable between the two approaches. Both require:
-
Retrieving the edited row
-
Pulling the selected values from the dropdowns
-
Executing your data-access logic
-
Rebinding the grid
The TemplateColumn again requires more manual control discovery, while the GridDropDownColumn lets you access the selected value directly through the column editor.
Private Sub RadGrid1_UpdateCommand(sender As Object, e As GridCommandEventArgs) Handles RadGrid1.UpdateCommand
Dim strError As String = ""
Try
Dim strSalesperson As String = ""
Dim strComments As String = ""
Dim intContactID As Int32 = 0
Dim editedItem As GridEditableItem = CType(e.Item, GridEditableItem)
'get the data key values so we can to the data access
Dim TrackingNo As String = RadGrid1.MasterTableView.DataKeyValues(e.Item.ItemIndex)("TrackingNo")
Dim strBidderID As String = RadGrid1.MasterTableView.DataKeyValues(e.Item.ItemIndex)("BidderID")
Dim intRowID As Int32 = RadGrid1.MasterTableView.DataKeyValues(e.Item.ItemIndex)("RowID")
'===================================================================
'handle PRODUCT GROUP
'===================================================================
'get the product group from the dropdown
Dim ddlProductGroup As RadDropDownList = editedItem.FindControl("ddlProductGroup")
Dim strProductGroup As String = ddlProductGroup.SelectedValue
'===================================================================
'handle SLPRSNID
'===================================================================
'get the salesperson from the dropdown
strSalesperson = ToString2(TryCast(editedItem("slprsnid").Controls(0), RadComboBox).SelectedValue)
'save
Dim oSB As New PTSpecificBidders(intRowID, App.ConnectionString)
If oSB.isPopulated Then
'to save the record
SPs.fp_PTSpecificBidders_INSUPD(TrackingNo, strBidderID, "GP", oSB.RevisedDate, oSB.SentBid, "", strComments, oSB.PriceLevel, "1/1/1900", Now, oSB.Quoting, strProductGroup, intContactID, strSalesperson, App.ConnectionString).execute()
End If
Me.RadGrid1.Rebind()
Catch ex As Threading.ThreadAbortException
Catch ex As Exception
ErrorHandler.globalErrorHandler(ex, strError, App.AppName, App.UserName, False)
Me.lblError.Text = ex.Message
End Try
End Sub