Joel 4/27/2012 5:17:15 PM

Telerik - Update grid example, bind a dropdown list in a template column example

This article will show how to update a Telerik ASP.NET AJAX grid. We'll look at gathering data from both template and non-template columns.

We'll also show how to bind a drop down list in the template column.


  1. ASP code:
    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" 
        EditMode="InPlace" CommandItemDisplay="Top"
        CssClass="grid" Skin="Web20"
        AllowPaging="True" PageSize="20"  Width="600px"
        <MasterTableView DataKeyNames="lRowID" CommandItemDisplay="Top" EditMode="InPlace">
                <telerik:GridEditCommandColumn   ButtonType="ImageButton"></telerik:GridEditCommandColumn>
                <telerik:GridBoundColumn Visible="false" DataField="LRowID"  ></telerik:GridBoundColumn>
                <telerik:GridBoundColumn Visible="false" DataField="sObjectName" ></telerik:GridBoundColumn>
                <telerik:GridBoundColumn HeaderText="Section" DataField="col1" ReadOnly="true" ></telerik:GridBoundColumn>
                <telerik:GridBoundColumn HeaderText="Form" DataField="col2" ReadOnly="true" ></telerik:GridBoundColumn>
                <telerik:GridTemplateColumn HeaderText="Permissions" UniqueName="Permissions">
                        <asp:Label ID="lblPerms" runat="server"></asp:Label>
                        <asp:DropDownList ID="ddlPerms" runat="server">
                            <asp:ListItem Value=0>None</asp:ListItem>
                            <asp:ListItem Value=1>Read Only</asp:ListItem>
                            <asp:ListItem Value=2>Full</asp:ListItem>


Code behind:

First, we bind the combobox in the ItemDatabound event:

    Protected Sub RadGrid1_ItemDataBound(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridItemEventArgs
Handles RadGrid1.ItemDataBound         If (TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode) Then             'if this line is in edit mode             Dim item As GridEditableItem = e.Item             'access/modify the edit item template settings here             Dim list As DropDownList = item.FindControl("ddlPerms")             'list.DataSource = getPermValues()             'list.DataBind()             list.SelectedValue = e.Item.DataItem("intPerms")         ElseIf (TypeOf e.Item Is GridDataItem AndAlso Not e.Item.IsInEditMode AndAlso Page.IsPostBack) Then             'if this line is not in edit mode             Dim item As GridDataItem = e.Item             Dim label As Label = item.FindControl("lblPerms")             label.Text = e.Item.DataItem("strPerms")         End If     End Sub

This code will handle the update. We show how to retrieve data from the template column and the other columns

    Protected Sub RadGrid1_UpdateCommand(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridCommandEventArgs
Handles RadGrid1.UpdateCommand         'get a reference to the line         Dim editedItem As GridEditableItem = CType(e.Item, GridEditableItem)         'get a reference to the ddl in the template control         Dim ddlPerms As DropDownList = editedItem.FindControl("ddlPerms")         Dim intPerms As Int16 = ddlPerms.SelectedValue         'get a hash table, and get the other column values         Dim oHashTable As Hashtable = New Hashtable         e.Item.OwnerTableView.ExtractValuesFromItem(oHashTable, editedItem)         'this datatable is the same as our datasource, but with no data in it         Dim oDT As DataTable = dynData.SPs.FP_Hierarchy_SEL(-1, AppUser.strLastDB).getTable         'by doing 'new row', we get exactly one row         Dim oRow As DataRow = oDT.NewRow         'start editing that row         oRow.BeginEdit()         Try             'loop through the hash table, assigning values to the data table             For Each entry As DictionaryEntry In oHashTable                 oRow(CType(entry.Key, String)) = entry.Value             Next             oRow.EndEdit()             'retrieve one value from the datarow             Dim intRowID As System.Int64 = oRow("lRowID").ToString()             'business logic             Dim bFullPerms As Boolean = False             Dim bReadPerms As Boolean = False             Select Case intPerms                 Case 1                     bReadPerms = True                 Case 2                     bFullPerms = True             End Select             If bFullPerms And bReadPerms Then                 Exit Sub             End If             'update the underlying dataset             dynData.SPs._4P_OEDGroups_UPD(Session("GroupID"), intRowID, 
bFullPerms, bReadPerms, AppUser.strLastDB).execute()             'rebind the grie             Me.RadGrid1.Rebind()         Catch ex As Exception             oRow.CancelEdit()             e.Canceled = True         End Try